developit / preact-jsx-chai

:white_check_mark: Add JSX assertions to Chai, with support for Preact Components.
http://npm.im/preact-jsx-chai
MIT License
56 stars 7 forks source link

how set context in test? #55

Closed TheOne1006 closed 6 years ago

TheOne1006 commented 6 years ago

how set context in test?

maciej-rosiek commented 6 years ago

just came here to find answer for this, couldn't find one so I did it like this:

class ContextProvider extends Component<{}, {}> {

    public getChildContext(): any {
        return {
            contextVariable: 'test'
        };
    }

    public render(): any {
        return <div>{this.props.children}</div>;
    }

}

expect(
    <ContextProvider>
        <ComponentUsinContextVariable/>
    </ContextProvider>
);
developit commented 6 years ago

There's an easy module that lets you do just that called preact-context-provider:

import Provider from 'preact-context-provider';

const foo = { some: 'stuff' };

expect(
  <Provider foo={foo}>
    <App />
  </Provider>
);