Skip to content

Instantly share code, notes, and snippets.

@ryandrewjohnson
Last active April 16, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryandrewjohnson/1869aa21106ab7f37fe91f4f227d932c to your computer and use it in GitHub Desktop.
Save ryandrewjohnson/1869aa21106ab7f37fe91f4f227d932c to your computer and use it in GitHub Desktop.
import React from 'react';
import Enzyme, { mount } from 'enzyme';
// ensure you're resetting modules before each test
beforeEach(() => {
jest.resetModules();
});
// Takes the context data we want to test, or uses defaults
const getLanguageSelectorWithContext = (context = {languages: ['en', 'fr', 'es'], activeLanguage: 'en'}) => {
// Will then mock the LocalizeContext module being used in our LanguageSelector component
jest.doMock('./LocalizeContext', () => {
return {
LocalizeContext: {
Consumer: (props) => props.children(context)
}
}
});
// you need to re-require after calling jest.doMock.
// return the updated LanguageSelector module that now includes the mocked context
return require('./LanguageSelector').LanguageSelector;
};
describe('<LanguageSelector />', () => {
it('should return default list of languages', () => {
// This will use the default context param since we pass nothing
const LanguageSelector = getLanguageSelectorWithContext();
const wrapper = mount(<LanguageSelector />);
expect(wrapper.find('li').length).toBe(3);
});
it('should render no languages', () => {
// Here we override the context with the values we want for this specific test
const LanguageSelector = getLanguageSelectorWithContext({languages: [], activeLanguage: null});
const wrapper = mount(<LanguageSelector />);
expect(wrapper.find('li').length).toBe(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment