From 6e4c1e4657e876615ae3067a4249a07dc2f0c4e1 Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Thu, 28 May 2020 10:05:18 -0700 Subject: [PATCH 1/2] Create block middleware mock --- src/testing/mocks/middleware/block.ts | 24 +++++++++ tests/testing/unit/mocks/middleware/all.ts | 1 + tests/testing/unit/mocks/middleware/block.tsx | 50 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/testing/mocks/middleware/block.ts create mode 100644 tests/testing/unit/mocks/middleware/block.tsx diff --git a/src/testing/mocks/middleware/block.ts b/src/testing/mocks/middleware/block.ts new file mode 100644 index 000000000..637ca388e --- /dev/null +++ b/src/testing/mocks/middleware/block.ts @@ -0,0 +1,24 @@ +import { create } from '../../../core/vdom'; +import { DefaultMiddlewareResult } from '../../../core/interfaces'; + +const factory = create(); + +export function createBlockMock(blocks: [Function, any][] = []) { + const blocksMockMap = new Map(blocks); + const mockBlockMiddleware = factory(() => { + return (block: any) => { + const mock = blocksMockMap.get(block); + if (mock) { + return mock; + } + return (() => {}) as any; + }; + }); + + function mockBlock(): DefaultMiddlewareResult { + return mockBlockMiddleware(); + } + return mockBlock; +} + +export default createBlockMock; diff --git a/tests/testing/unit/mocks/middleware/all.ts b/tests/testing/unit/mocks/middleware/all.ts index d237b4887..5a28e381b 100644 --- a/tests/testing/unit/mocks/middleware/all.ts +++ b/tests/testing/unit/mocks/middleware/all.ts @@ -6,3 +6,4 @@ import './store'; import './breakpoint'; import './icache'; import './validity'; +import './block'; diff --git a/tests/testing/unit/mocks/middleware/block.tsx b/tests/testing/unit/mocks/middleware/block.tsx new file mode 100644 index 000000000..46ead3200 --- /dev/null +++ b/tests/testing/unit/mocks/middleware/block.tsx @@ -0,0 +1,50 @@ +const { it } = intern.getInterface('bdd'); +const { describe } = intern.getPlugin('jsdom'); +import createBlockMock from '../../../../../src/testing/mocks/middleware/block'; +import block from '../../../../../src/core/middleware/block'; +import { tsx, create } from '../../../../../src/core/vdom'; +import renderer, { assertion } from '../../../../../src/testing/renderer'; + +describe('block mock', () => { + it('should mock block middleware calls', () => { + function func() { + return null; + } + const blockMock = createBlockMock([[func, () => 'func result']]); + const factory = create({ block }); + const App = factory(({ middleware: { block } }) => { + const blockResult = block(func)(); + return
{blockResult}
; + }); + const r = renderer(() => , { middleware: [[block, blockMock]] }); + r.expect(assertion(() =>
func result
)); + }); + + it('should deal with multiple mocked functions', () => { + function func(_: any) { + return null; + } + function func1() { + return null; + } + const blockMock = createBlockMock([[func, (foo: any) => foo], [func1, () => 'bar']]); + const factory = create({ block }); + const App = factory(({ middleware: { block } }) => { + return ( +
+
{block(func)('foo')}
+
{block(func1)()}
+
+ ); + }); + const r = renderer(() => , { middleware: [[block, blockMock]] }); + r.expect( + assertion(() => ( +
+
foo
+
bar
+
+ )) + ); + }); +}); From fb7e75f5c3670a709b97d5e271904f3e50be37b4 Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Thu, 28 May 2020 10:53:26 -0700 Subject: [PATCH 2/2] Another block middleware mock test --- tests/testing/unit/mocks/middleware/block.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testing/unit/mocks/middleware/block.tsx b/tests/testing/unit/mocks/middleware/block.tsx index 46ead3200..7d04ba564 100644 --- a/tests/testing/unit/mocks/middleware/block.tsx +++ b/tests/testing/unit/mocks/middleware/block.tsx @@ -47,4 +47,18 @@ describe('block mock', () => { )) ); }); + + it('should return a default', () => { + function func() { + return null; + } + const blockMock = createBlockMock(); + const factory = create({ block }); + const App = factory(({ middleware: { block } }) => { + const blockResult = block(func)(); + return
{blockResult}
; + }); + const r = renderer(() => , { middleware: [[block, blockMock]] }); + r.expect(assertion(() =>
)); + }); });