This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
122cb52
commit 4af7591
Showing
5 changed files
with
137 additions
and
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export type PropList<T> = Extract<keyof T, string>[] | string; | ||
export type SubscribeCallback<T> = (el: any, props: PropList<T>) => () => void; | ||
export type SubscribeCallback<T> = (el: any, props: PropList<T>) => void; | ||
export type ConsumerRenderer<T> = (subscribe: SubscribeCallback<T>, renderer: Function) => any; |
126 changes: 0 additions & 126 deletions
126
packages/state-tunnel/src/utils/__test__/state-tunnel.spec.ts
This file was deleted.
Oops, something went wrong.
118 changes: 118 additions & 0 deletions
118
packages/state-tunnel/src/utils/__test__/state-tunnel.spec.tsx
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,118 @@ | ||
import { Component, Prop } from '@stencil/core'; | ||
import { newSpecPage, mockDocument } from '@stencil/core/testing'; | ||
import { createProviderConsumer } from '../state-tunnel'; | ||
|
||
describe('createProviderConsumer', () => { | ||
let Tunnel: any; | ||
let createTunnelCallback: any; | ||
let subscribe: any; | ||
let elm: any; | ||
let doc = mockDocument(); | ||
|
||
interface State { | ||
messageLog: string[], | ||
creatingMessage: boolean, | ||
setCreatingMessage: () => void | ||
} | ||
|
||
beforeEach(() => { | ||
elm = doc.createElement('stencil-element'); | ||
|
||
createTunnelCallback = jest.fn((subscribeFn) => { | ||
subscribe = subscribeFn; | ||
}); | ||
Tunnel = createProviderConsumer<State>({ | ||
messageLog: [], | ||
creatingMessage: false, | ||
setCreatingMessage: () => {} | ||
}, createTunnelCallback); | ||
}); | ||
|
||
it('should have a provider and consumer', () => { | ||
expect(Tunnel.Provider).toBeDefined(); | ||
expect(Tunnel.Consumer).toBeDefined(); | ||
}); | ||
|
||
it('should call Tunnel consumerRender when Consumer created', () => { | ||
const props = {}; | ||
const childAsFunction = jest.fn(); | ||
Tunnel.Consumer(props, [childAsFunction]); | ||
subscribe(elm, 'all'); | ||
|
||
expect(createTunnelCallback.mock.calls.length).toBe(1); | ||
expect(createTunnelCallback.mock.calls[0][1]).toBe(childAsFunction); | ||
}); | ||
|
||
it('should set element prop as copy of state when only a string is passed', () => { | ||
Tunnel.Consumer({}, [jest.fn()]); | ||
subscribe(elm, 'all'); | ||
|
||
const newState = { | ||
messageLog: ['a'], | ||
creatingMessage: false, | ||
setCreatingMessage: () => {} | ||
} | ||
|
||
Tunnel.Provider({ state: newState }); | ||
expect(elm.all).toEqual(newState); | ||
}); | ||
|
||
it('should set element props array of strings', () => { | ||
Tunnel.Consumer({}, [jest.fn()]); | ||
subscribe(elm, ['messageLog', 'creatingMessage']); | ||
|
||
const newState = { | ||
messageLog: ['a'], | ||
creatingMessage: false, | ||
setCreatingMessage: () => {} | ||
} | ||
|
||
Tunnel.Provider({ state: newState }); | ||
expect(elm.messageLog).toEqual(['a']); | ||
expect(elm.creatingMessage).toEqual(false); | ||
expect(elm.setCreatingMessage).toBeUndefined(); | ||
}); | ||
|
||
fit('should set element props array of strings', async () => { | ||
@Component({ tag: 'cmp-a' }) | ||
class CmpA { | ||
@Prop() messageLog: string[]; | ||
@Prop() creatingMessage: boolean; | ||
} | ||
|
||
Tunnel.injectProps(CmpA, ['messageLog', 'creatingMessage']); | ||
|
||
const { root } = await newSpecPage({ | ||
components: [CmpA], | ||
html: `<cmp-a></cmp-a>` | ||
}); | ||
|
||
expect(root.messageLog).toEqual([]); | ||
expect(root.creatingMessage).toEqual(false); | ||
|
||
let newState = { | ||
messageLog: ['a'], | ||
creatingMessage: true, | ||
setCreatingMessage: () => {} | ||
} | ||
|
||
Tunnel.Provider({ state: newState }); | ||
|
||
expect(root.messageLog).toEqual(['a']); | ||
expect(root.creatingMessage).toEqual(true); | ||
expect(root.setCreatingMessage).toBeUndefined(); | ||
|
||
newState = { | ||
messageLog: ['b'], | ||
creatingMessage: false, | ||
setCreatingMessage: () => {} | ||
} | ||
|
||
Tunnel.Provider({ state: newState }); | ||
|
||
expect(root.messageLog).toEqual(['b']); | ||
expect(root.creatingMessage).toEqual(false); | ||
expect(root.setCreatingMessage).toBeUndefined(); | ||
}); | ||
|
||
}); |
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