This repository has been archived by the owner on Dec 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add state-tunnel mock to fix testing issue: ionic-team/stencil-state-…
- Loading branch information
Showing
2 changed files
with
125 additions
and
7 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
packages/playground/__mocks__/@stencil/state-tunnel.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,122 @@ | ||
import { FunctionalComponent } from "@stencil/core"; | ||
|
||
import { SubscribeCallback } from "../declarations"; | ||
|
||
function defaultConsumerRender( | ||
subscribe: SubscribeCallback<string>, | ||
renderer: Function | ||
) { | ||
return <context-consumer subscribe={subscribe} renderer={renderer} />; | ||
} | ||
|
||
export function createProviderConsumer<T extends object>( | ||
defaultState: T, | ||
consumerRender = defaultConsumerRender | ||
) { | ||
type PropList = Extract<keyof T, string>[]; | ||
|
||
const listeners: Map<HTMLStencilElement, PropList> = new Map(); | ||
let currentState: T = defaultState; | ||
|
||
function notifyConsumers() { | ||
listeners.forEach(updateListener); | ||
} | ||
|
||
function updateListener(fields: PropList, listener: HTMLStencilElement) { | ||
if (Array.isArray(fields)) { | ||
[...fields].forEach(fieldName => { | ||
(listener as any)[fieldName] = currentState[fieldName]; | ||
}); | ||
} else { | ||
(listener as any)[fields] = { | ||
...(currentState as object) | ||
} as T; | ||
} | ||
listener.forceUpdate(); | ||
} | ||
|
||
function attachListener(propList: PropList) { | ||
return (el: HTMLStencilElement) => { | ||
if (listeners.has(el)) { | ||
return; | ||
} | ||
listeners.set(el, propList); | ||
updateListener(propList, el); | ||
}; | ||
} | ||
|
||
function subscribe(el: HTMLStencilElement, propList: PropList) { | ||
attachListener(propList)(el); | ||
return function() { | ||
listeners.delete(el); | ||
}; | ||
} | ||
|
||
const Provider: FunctionalComponent<{ state: T }> = ({ state }, children) => { | ||
currentState = state; | ||
notifyConsumers(); | ||
return children; | ||
}; | ||
|
||
const Consumer: FunctionalComponent<{}> = (props, children) => { | ||
// The casting on subscribe is to allow for crossover through the stencil compiler | ||
// In the future we should allow for generics in components. | ||
return consumerRender( | ||
subscribe as SubscribeCallback<string>, | ||
children[0] as Function | ||
); | ||
}; | ||
|
||
function wrapConsumer(childComponent: any, fieldList: PropList) { | ||
const Child = childComponent.is; | ||
|
||
return ({ children, ...props }: any) => { | ||
return ( | ||
<Child ref={attachListener(fieldList)} {...props}> | ||
{children} | ||
</Child> | ||
); | ||
}; | ||
} | ||
|
||
function injectProps(childComponent: any, fieldList: PropList) { | ||
let unsubscribe: any = null; | ||
|
||
const elementRefName = Object.keys(childComponent.properties).find( | ||
propName => { | ||
return childComponent.properties[propName].elementRef == true; | ||
} | ||
); | ||
if (elementRefName == undefined) { | ||
throw new Error( | ||
`Please ensure that your Component ${ | ||
childComponent.is | ||
} has an attribute with an "@Element" decorator. ` + | ||
`This is required to be able to inject properties.` | ||
); | ||
} | ||
|
||
const prevComponentWillLoad = childComponent.prototype.componentWillLoad; | ||
childComponent.prototype.componentWillLoad = function() { | ||
unsubscribe = subscribe(this[elementRefName], fieldList); | ||
if (prevComponentWillLoad) { | ||
return prevComponentWillLoad.bind(this)(); | ||
} | ||
}; | ||
|
||
const prevComponentDidUnload = childComponent.prototype.componentDidUnload; | ||
childComponent.prototype.componentDidUnload = function() { | ||
unsubscribe(); | ||
if (prevComponentDidUnload) { | ||
return prevComponentDidUnload.bind(this)(); | ||
} | ||
}; | ||
} | ||
|
||
return { | ||
Provider, | ||
Consumer, | ||
wrapConsumer, | ||
injectProps | ||
}; | ||
} |
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