Update gettext unit tests to clearly test their functionality

- instead of mocking the Django global functions, test each function's export that it correctly uses the global if present or test the default behaviour
pull/9600/head
LB Johnston 2022-11-06 21:36:47 +10:00 zatwierdzone przez LB (Ben Johnston)
rodzic e609f15a93
commit acd412e8f3
1 zmienionych plików z 42 dodań i 36 usunięć

Wyświetl plik

@ -9,68 +9,74 @@ import {
const STRING = const STRING =
'The name wagtail stems from the constant sideways wagging of the tail.'; 'The name wagtail stems from the constant sideways wagging of the tail.';
afterEach(() => {
// clear any mocked globals after each test
if (window.django) {
delete window.django;
}
});
describe('gettext', () => { describe('gettext', () => {
beforeEach(() => { it('should return the provided string if Django gettext is not loaded', () => {
window.django = { gettext: jest.fn((val) => val) }; expect(gettext(STRING)).toEqual(STRING);
}); });
it('should return the value based on the django util', () => { it('should call the global Django util if loaded', () => {
expect(window.django.gettext).not.toHaveBeenCalled(); const gettextMock = jest.fn();
expect(gettext(STRING)).toEqual(STRING); window.django = { gettext: gettextMock };
expect(window.django.gettext).toHaveBeenCalledWith(STRING); gettext(STRING);
expect(gettextMock).toHaveBeenCalledWith(STRING);
}); });
}); });
describe('ngettext', () => { describe('ngettext', () => {
beforeEach(() => { it('should return the first param if Django ngettext is not loaded', () => {
window.django = { ngettext: jest.fn((val) => val) }; expect(ngettext('One bird', 'Many birds', 2)).toEqual('One bird');
}); });
it('should return the value based on the django util', () => { it('should call the global Django util if loaded', () => {
expect(window.django.ngettext).not.toHaveBeenCalled(); const ngettextMock = jest.fn();
expect(ngettext('One bird', 'Many birds', 2)).toEqual('One bird'); window.django = { ngettext: ngettextMock };
expect(window.django.ngettext).toHaveBeenCalledWith( ngettext('One bird', 'Many birds', 2);
'One bird', expect(ngettextMock).toHaveBeenCalledWith('One bird', 'Many birds', 2);
'Many birds',
2,
);
}); });
}); });
describe('getFormat', () => { describe('getFormat', () => {
beforeEach(() => { it('should return an empty string if Django get_format is not loaded', () => {
window.django = { get_format: jest.fn(() => 1) }; expect(getFormat('FIRST_DAY_OF_WEEK')).toEqual('');
}); });
it('should return the value based on the django util', () => { it('should call the global Django util if loaded', () => {
expect(window.django.get_format).not.toHaveBeenCalled(); const getFormatMock = jest.fn();
expect(getFormat('FIRST_DAY_OF_WEEK')).toEqual(1); window.django = { get_format: getFormatMock };
expect(window.django.get_format).toHaveBeenCalledWith('FIRST_DAY_OF_WEEK'); getFormat('FIRST_DAY_OF_WEEK');
expect(getFormatMock).toHaveBeenCalledWith('FIRST_DAY_OF_WEEK');
}); });
}); });
describe('gettextNoop', () => { describe('gettextNoop', () => {
beforeEach(() => { it('should return the provided string if Django gettext_noop is not loaded', () => {
window.django = { gettext_noop: jest.fn((val) => val) }; expect(gettextNoop(STRING)).toEqual(STRING);
}); });
it('should return the value based on the django util', () => { it('should call the global Django util if loaded', () => {
expect(window.django.gettext_noop).not.toHaveBeenCalled(); const gettextNoopMock = jest.fn();
expect(gettextNoop(STRING)).toEqual(STRING); window.django = { gettext_noop: gettextNoopMock };
expect(window.django.gettext_noop).toHaveBeenCalledWith(STRING); gettextNoop(STRING);
expect(gettextNoopMock).toHaveBeenCalledWith(STRING);
}); });
}); });
describe('pluralIdx', () => { describe('pluralIdx', () => {
beforeEach(() => { it('should return false if if Django pluralidx is not loaded', () => {
window.django = { expect(pluralIdx(3)).toEqual(false);
pluralidx: jest.fn((val) => ({ 0: true, 1: false }[val] || true)),
};
}); });
it('should return the value based on the django util', () => { it('should call the global Django util if loaded', () => {
expect(window.django.pluralidx).not.toHaveBeenCalled(); const pluralidxMock = jest.fn();
expect(pluralIdx(0)).toEqual(true); window.django = { pluralidx: pluralidxMock };
expect(window.django.pluralidx).toHaveBeenCalledWith(0); pluralIdx(5);
expect(pluralidxMock).toHaveBeenCalledWith(5);
}); });
}); });