window.navigator
and its properties are read-only, this is the reason why Object.defineProperty
is needed to set window.navigator.language
. It’s supposed to work for changing property value multiple times.
The problem is that the component is already instantiated in beforeEach
, window.navigator.language
changes don’t affect it.
Using Object.defineProperty
for mocking properties manually will require to store original descriptor and restore it manually as well. This can be done with jest.spyOn
. jest.clearAllMocks()
wouldn’t help for manual spies/mocks, it may be unneeded for Jest spies.
It likely should be:
let languageGetter;
beforeEach(() => {
languageGetter = jest.spyOn(window.navigator, 'language', 'get')
})
it('should do thing 1', () => {
languageGetter.mockReturnValue('de')
wrapper = shallow(<Component {...props} />)
expect(wrapper.state('currentLanguage')).toEqual('de')
})
...