I'm trying to manipulate documentElement lang property for my tests. And can't figure it out how can I accomplished that. I've already tried defining setupFiles in Jest config - and that gave me possibility to define it and no option to change it for different tests - as described here: Mocking `document` in jest
Part of testing code looks like:
const lang: string = document.documentElement.lang ?
document.documentElement.lang : Constraints.DEFAULT_LANGUAGE;
The test look like this:
test('should pass `en` language when document hasn`t any specified', () => {
const spy = jest.spyOn(window.document, 'documentElement', 'get');
expect(spy).toHaveBeenCalled();
});
And with setupFiles:
Object.defineProperty(document, 'documentElement', {
writable: true,
configurable: true,
value: document.createElement('document')
});
When I have setupFiles I get error:
Property documentElement does not have access type get
But when I try to spy on it without setupfile.js
configured, spy is never called.
EDIT
More clear example what I'm trying to achieve:
const lang: string = document.documentElement.lang ?
document.documentElement.lang : Constraints.DEFAULT_LANGUAGE;
component.src = `/${lang}/mysite`;
test('should pass `de` language when document has one specified', () => {
const spy = jest.spyOn(window.document, 'documentElement', 'get');
const mockElement = document.createElement('document');
mockElement.lang = 'de';
spy.mockReturnValue(mockElement);
expect(component.src).toContain('/de/');
});
And in test I'm getting:
expect(received).toContain(expected) // indexOf
Expected substring: "/de/"
Received string: "http://localhost/en/mysite"