Skip to content

Commit

Permalink
1.0.7 - Allow for undefined properties in the session data
Browse files Browse the repository at this point in the history
  • Loading branch information
techbech committed Jul 21, 2020
1 parent 5fd2ffa commit 4fd82ff
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
4 changes: 0 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions dist/index.modern.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.modern.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dispatch, SetStateAction } from "react";
import { Dispatcher } from "./dispatcher";
export declare type SessionValueType = {} | null;
export declare type SessionValueType = {} | null | undefined;
export declare type SessionGenericData = {
[key: string]: SessionValueType;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@peteck/react-context-session",
"version": "1.0.6",
"version": "1.0.7",
"description": "Arbitrary session data with support for multiple contexts",
"author": "Peteck",
"license": "MIT",
Expand Down
34 changes: 27 additions & 7 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type SessionData = {
a: number;
b: number;
c: string;
d?: boolean;
};

const useSession = useSessionImpl<SessionData>();
Expand Down Expand Up @@ -49,12 +50,19 @@ function TestComponent1() {
}

function TestComponent2() {
const [{ c }, set] = useSession(["c"]);
const [{ c, d }, set] = useSession(["c", "d"]);

return (
<div>
<p id="c">{c}</p>
<button id="btn2" onClick={() => set("c", String(Number(c) + 1))} />
<p id="d">{d === true ? "true" : "false"}</p>
<button
id="btn2"
onClick={() => {
set("c", String(Number(c) + 1));
set("d", true);
}}
/>
</div>
);
}
Expand All @@ -64,19 +72,26 @@ function TestComponent3() {

return (
<div>
<button id="btn3" onClick={() => set("c", "TEST")} />
<button
id="btn3"
onClick={() => {
set("c", "TEST");
set("d", undefined);
}}
/>
</div>
);
}

function ObserverComponent() {
const [{ a, b, c }] = useSession(["b", "c", "a"]);
const [{ a, b, c, d }] = useSession(["b", "c", "a", "d"]);

return (
<div>
<span id="o-a">{a}</span>
<span id="o-b">{b}</span>
<span id="o-c">{c}</span>
<span id="o-d">{typeof d}</span>
</div>
);
}
Expand All @@ -101,14 +116,18 @@ function TestApp({
);
}

function testData(a: number, b: number, c: string) {
function testData(a: number, b: number, c: string, d?: boolean) {
expect(Number(container.querySelector("#a").innerHTML)).toBe(a);
expect(Number(container.querySelector("#b").innerHTML)).toBe(b);
expect(container.querySelector("#c").innerHTML).toBe(c);
expect(container.querySelector("#d").innerHTML).toBe(
d === true ? "true" : "false",
);

expect(Number(container.querySelector("#o-a").innerHTML)).toBe(a);
expect(Number(container.querySelector("#o-b").innerHTML)).toBe(b);
expect(container.querySelector("#o-c").innerHTML).toBe(c);
expect(container.querySelector("#o-d").innerHTML).toBe(typeof d);
}

it("can not use useSession hook without session provider", () => {
Expand All @@ -120,7 +139,7 @@ it("data has the correct default values", () => {
act(() => {
render(<TestApp />, container);
});
testData(defaultData.a, defaultData.b, defaultData.c);
testData(defaultData.a, defaultData.b, defaultData.c, defaultData.d);
});

it("can set session values", () => {
Expand All @@ -140,13 +159,14 @@ it("can set session values", () => {
defaultData.a + 1,
defaultData.b + 1,
String(Number(defaultData.c) + 1),
true,
);

act(() => {
btn3.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});

testData(defaultData.a + 1, defaultData.b + 1, "TEST");
testData(defaultData.a + 1, defaultData.b + 1, "TEST", defaultData.d);
});

it("can mount and dismount", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dispatch, SetStateAction } from "react";
import { Dispatcher } from "./dispatcher";

export type SessionValueType = {} | null;
export type SessionValueType = {} | null | undefined;
export type SessionGenericData = { [key: string]: SessionValueType };
export type SessionDispatchFunc = Dispatch<SetStateAction<any>>;
export type SessionContextKey = string;
Expand Down
6 changes: 0 additions & 6 deletions src/use-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ export function useSessionBase<

const set = useCallback(
<P extends keyof DataType>(key: P, value: DataType[P]) => {
if (typeof context.data[key] === "undefined") {
throw new Error(
`"${key}" is not defined in the default session values. Make sure it's present and try again.`,
);
}

context.data[key] = value;

if (context.onChange) {
Expand Down

0 comments on commit 4fd82ff

Please sign in to comment.