Skip to content

Instantly share code, notes, and snippets.

@edtoken
Last active April 8, 2019 10:21
Show Gist options
  • Save edtoken/721e9c7b68afd853196d381c9b01a5c0 to your computer and use it in GitHub Desktop.
Save edtoken/721e9c7b68afd853196d381c9b01a5c0 to your computer and use it in GitHub Desktop.
import { Arg, Substitute } from '@fluffy-spoon/substitute';
/**
* @see https://www.npmjs.com/package/@fluffy-spoon/substitute
*/
describe('Mock functionality works correctly', () => {
it('tmp', () => {
interface Element {
readonly destroy: () => void;
}
interface ParentMethod {
readonly mount: () => Element;
}
interface ChildMethod1 extends ParentMethod {
readonly name: string;
}
interface ChildMethod2 extends ParentMethod {
readonly name: string;
}
interface Api {
readonly child1: ChildMethod1;
readonly child2: ChildMethod2;
}
const api = Substitute.for<Api>();
const spy1 = jest.fn();
const spy2 = jest.fn();
// @ts-ignore
api.child1.mount().returns(Substitute.for<Element>());
// @ts-ignore
api.child2.mount().returns(Substitute.for<Element>());
api.child1
.mount()
.destroy()
// @ts-ignore
.mimicks(spy1);
api.child2
.mount()
.destroy()
// @ts-ignore
.mimicks(spy2);
const el = api.child1.mount();
el.destroy();
expect(spy1).toBeCalledTimes(1);
expect(spy2).not.toHaveBeenCalled();
interface ComponentProps {
readonly api: Api;
}
interface ComponentState {
readonly element: Element
}
class TmpParentComponent extends React.Component<ComponentProps, ComponentState> {
componentDidMount() {
this.setState({ element: this.props.api.child1.mount() });
}
componentWillUnmount() {
this.state.element.destroy();
}
render() {
return <div/>;
}
}
const wrapper = mount(<TmpParentComponent api={api}/>);
expect(spy1).toBeCalledTimes(1);
expect(spy2).not.toHaveBeenCalled();
wrapper.unmount();
expect(spy1).toBeCalledTimes(2);
expect(spy2).not.toHaveBeenCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment