-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from saasquatch/component-scope-fix
Fix component scope in bundles
- Loading branch information
Showing
8 changed files
with
175 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ComponentScope as VanillaComponentScope } from "../dist/vanilla.js"; | ||
import { ComponentScope as VanillaComponentScopeMjs } from "../dist/vanilla.mjs"; | ||
|
||
import { ComponentScope as ReactComponentScope } from "../dist/react.js"; | ||
import { ComponentScope as ReactComponentScopeMjs } from "../dist/react.mjs"; | ||
|
||
import { ComponentScope as VueComponentScope } from "../dist/vue.js"; | ||
import { ComponentScope as VueComponentScopeMjs } from "../dist/vue.mjs"; | ||
|
||
import { createScope as VanillaCreateScope } from "../dist/vanilla.js"; | ||
import { createScope as VanillaCreateScopeMjs } from "../dist/vanilla.mjs"; | ||
|
||
import { createScope as ReactCreateScope } from "../dist/react.js"; | ||
import { createScope as ReactCreateScopeMjs } from "../dist/react.mjs"; | ||
|
||
import { createScope as VueCreateScope } from "../dist/vue.js"; | ||
import { createScope as VueCreateScopeMjs } from "../dist/vue.mjs"; | ||
|
||
import { molecule as VanillaMolecule } from "../dist/vanilla.js"; | ||
import { molecule as VanillaMoleculeMjs } from "../dist/vanilla.mjs"; | ||
|
||
import { molecule as ReactMolecule } from "../dist/react.js"; | ||
import { molecule as ReactMoleculeMjs } from "../dist/react.mjs"; | ||
|
||
import { molecule as VueMolecule } from "../dist/vue.js"; | ||
import { molecule as VueMoleculeMjs } from "../dist/vue.mjs"; | ||
|
||
/** | ||
* | ||
* LANDMINE: This requires build to have run first!! | ||
* | ||
*/ | ||
describe("Build artifacts", () => { | ||
describe("Vue bundle", () => { | ||
test("Exports are the same in CommonJS", () => { | ||
expect(VueMolecule).toBe(VanillaMolecule); | ||
expect(VueCreateScope).toBe(VanillaCreateScope); | ||
// Test of the fix for https://github.com/saasquatch/bunshi/issues/29 | ||
expect(VueComponentScope).toBe(VanillaComponentScope); | ||
}); | ||
|
||
test("Exports are the same in MJS", () => { | ||
expect(VueMoleculeMjs).toBe(VanillaMoleculeMjs); | ||
expect(VueCreateScopeMjs).toBe(VanillaCreateScopeMjs); | ||
// Test of the fix for https://github.com/saasquatch/bunshi/issues/29 | ||
expect(VueComponentScopeMjs).toBe(VanillaComponentScopeMjs); | ||
}); | ||
}); | ||
|
||
describe("React bundle", () => { | ||
test("Exports are the same in CommonJS for React", () => { | ||
expect(ReactMolecule).toBe(VanillaMolecule); | ||
expect(ReactCreateScope).toBe(VanillaCreateScope); | ||
// Test of the fix for https://github.com/saasquatch/bunshi/issues/29 | ||
expect(ReactComponentScope).toBe(VanillaComponentScope); | ||
}); | ||
|
||
test("Exports are the same in MJS", () => { | ||
expect(ReactMoleculeMjs).toBe(VanillaMoleculeMjs); | ||
expect(ReactCreateScopeMjs).toBe(VanillaCreateScopeMjs); | ||
// Test of the fix for https://github.com/saasquatch/bunshi/issues/29 | ||
expect(ReactComponentScopeMjs).toBe(VanillaComponentScopeMjs); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,116 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { act, renderHook } from "@testing-library/react-hooks"; | ||
import { atom, useAtom } from "jotai"; | ||
import { ComponentScope, molecule } from "./"; | ||
import { createTestInjectorProvider } from './internal/TestInjectorProvider'; | ||
import { createTestInjectorProvider } from "./internal/TestInjectorProvider"; | ||
import { useMolecule } from "./useMolecule"; | ||
|
||
const ComponentScopedCountMolecule = molecule((_, scope) => { | ||
scope(ComponentScope); | ||
return atom(0); | ||
scope(ComponentScope); | ||
return atom(0); | ||
}); | ||
|
||
const GlobalScopedMoleculeCountMolecule = molecule((_, scope) => { | ||
return atom(0); | ||
return atom(0); | ||
}); | ||
|
||
const useCounter = (mol: typeof ComponentScopedCountMolecule) => { | ||
const [count, setCount] = useAtom(useMolecule(mol)) | ||
return { | ||
count, | ||
increment: () => setCount(c => c + 1) | ||
} | ||
const [count, setCount] = useAtom(useMolecule(mol)); | ||
return { | ||
count, | ||
increment: () => setCount((c) => c + 1), | ||
}; | ||
}; | ||
|
||
|
||
describe("Global scoped molecules", () => { | ||
test('should increment counter', () => { | ||
testOneCounter(GlobalScopedMoleculeCountMolecule, 1); | ||
}) | ||
test('two counters should be connected for global scope', () => { | ||
// Note: This is an important test case, because | ||
// it makes sure that our `testTwoCounters` function | ||
// can tell the difference between a globally | ||
// scoped molecule and not component-scope molecule | ||
testTwoCounters(GlobalScopedMoleculeCountMolecule, 2); | ||
}) | ||
}) | ||
test("should increment counter", () => { | ||
testOneCounter(GlobalScopedMoleculeCountMolecule, 1); | ||
}); | ||
test("two counters should be connected for global scope", () => { | ||
// Note: This is an important test case, because | ||
// it makes sure that our `testTwoCounters` function | ||
// can tell the difference between a globally | ||
// scoped molecule and not component-scope molecule | ||
testTwoCounters(GlobalScopedMoleculeCountMolecule, { | ||
actual1: true, | ||
actual2: true, | ||
expected1: 2, | ||
expected2: 2, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("Component scoped molecules", () => { | ||
test('should increment counter', () => { | ||
testOneCounter(ComponentScopedCountMolecule, 1); | ||
}) | ||
test('two counters should be not be connected when component scoped', () => { | ||
testTwoCounters(ComponentScopedCountMolecule, 1); | ||
}) | ||
|
||
}) | ||
|
||
function testOneCounter(mol: typeof ComponentScopedCountMolecule, expectedResult: number) { | ||
|
||
const TestInjectorProvider = createTestInjectorProvider(); | ||
|
||
const { result } = renderHook(() => useCounter(mol), { wrapper: TestInjectorProvider }); | ||
|
||
act(() => { | ||
result.current.increment(); | ||
test("should increment counter", () => { | ||
testOneCounter(ComponentScopedCountMolecule, 1); | ||
}); | ||
test("two counters should be not be connected when component scoped", () => { | ||
testTwoCounters(ComponentScopedCountMolecule, { | ||
actual1: true, | ||
actual2: true, | ||
expected1: 1, | ||
expected2: 1, | ||
}); | ||
}); | ||
test("two counters should be not be connected when component scoped, only one", () => { | ||
testTwoCounters(ComponentScopedCountMolecule, { | ||
actual1: true, | ||
actual2: false, | ||
expected1: 1, | ||
expected2: 0, | ||
}); | ||
}); | ||
test("two counters should be not be connected when component scoped, only one", () => { | ||
testTwoCounters(ComponentScopedCountMolecule, { | ||
actual1: false, | ||
actual2: true, | ||
expected1: 0, | ||
expected2: 1, | ||
}); | ||
}); | ||
}); | ||
|
||
expect(result.current.count).toBe(expectedResult); | ||
} | ||
|
||
function testTwoCounters(mol: typeof ComponentScopedCountMolecule, expectedResult: number) { | ||
function testOneCounter( | ||
mol: typeof ComponentScopedCountMolecule, | ||
expectedResult: number | ||
) { | ||
const TestInjectorProvider = createTestInjectorProvider(); | ||
|
||
const TestInjectorProvider = createTestInjectorProvider(); | ||
const { result } = renderHook(() => useCounter(mol), { | ||
wrapper: TestInjectorProvider, | ||
}); | ||
|
||
const { result: result1 } = renderHook(() => useCounter(mol), { wrapper: TestInjectorProvider }); | ||
const { result: result2 } = renderHook(() => useCounter(mol), { wrapper: TestInjectorProvider }); | ||
act(() => { | ||
result.current.increment(); | ||
}); | ||
|
||
act(() => { | ||
result1.current.increment(); | ||
}); | ||
act(() => { | ||
result2.current.increment(); | ||
}); | ||
expect(result.current.count).toBe(expectedResult); | ||
} | ||
|
||
expect(result1.current.count).toBe(expectedResult); | ||
expect(result2.current.count).toBe(expectedResult); | ||
function testTwoCounters( | ||
mol: typeof ComponentScopedCountMolecule, | ||
opts: { | ||
actual1: boolean; | ||
actual2: boolean; | ||
expected2: number; | ||
expected1: number; | ||
} | ||
) { | ||
const TestInjectorProvider = createTestInjectorProvider(); | ||
|
||
const { result: result1 } = renderHook(() => useCounter(mol), { | ||
wrapper: TestInjectorProvider, | ||
}); | ||
const { result: result2 } = renderHook(() => useCounter(mol), { | ||
wrapper: TestInjectorProvider, | ||
}); | ||
|
||
act(() => { | ||
opts.actual1 && result1.current.increment(); | ||
}); | ||
act(() => { | ||
opts.actual2 && result2.current.increment(); | ||
}); | ||
|
||
expect(result1.current.count).toBe(opts.expected1); | ||
expect(result2.current.count).toBe(opts.expected2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters