Last active
April 16, 2018 15:38
-
-
Save ryandrewjohnson/1869aa21106ab7f37fe91f4f227d932c to your computer and use it in GitHub Desktop.
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
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