Skip to content

Commit

Permalink
test: use en-GB locale in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khmm12 committed Sep 24, 2024
1 parent ec0747f commit 6c709d0
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/components/Time/Time.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ vi.mock('@/hooks/useCurrentLanguage')
beforeEach(() => {
vi.useFakeTimers()
vi.setSystemTime(new Date('2022-03-05T16:05:30'))
vi.mocked(useCurrentLanguage).mockReturnValue(() => 'ru-RU')
vi.mocked(useCurrentLanguage).mockReturnValue(() => 'en-GB')
})

afterEach(() => {
Expand All @@ -21,7 +21,7 @@ describe('Time', () => {

const date = screen.getByLabelText('Date')
expect(date).toBeDefined()
expect(date).toHaveTextContent('05.03.2022')
expect(date).toHaveTextContent('05/03/2022')
})

it('renders current time', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/TimeMilestones/TimeMilestones.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ beforeEach(() => {
}),
)

vi.mocked(useCurrentLanguage).mockReturnValue(() => 'en-US')
vi.mocked(useCurrentLanguage).mockReturnValue(() => 'en-GB')
vi.mocked(createCurrentDateTime).mockReturnValue(() => new Date('2022-03-05T16:05:30'))
vi.mocked(Milestone).mockImplementation((props) => (
<div aria-label={props.description}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Milestone, { MilestoneVariant } from '.'
vi.mock('@/hooks/useCurrentLanguage')

beforeEach(() => {
vi.mocked(useCurrentLanguage).mockReturnValue(() => 'en-US')
vi.mocked(useCurrentLanguage).mockReturnValue(() => 'en-GB')
})

afterEach(() => {
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/useCurrentLanguage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ afterEach(() => {
describe('useCurrentLanguage', () => {
it('returns current language', () => {
vi.mocked(getCurrentLocale).mockReturnValue('en-US')
const currentLanguage = renderHook(() => useCurrentLanguage()).result
const hook1 = renderHook(() => useCurrentLanguage())

expect(currentLanguage()).toBe('en-US')
expect(hook1.result()).toBe('en-US')

vi.mocked(getCurrentLocale).mockReturnValue('ru-RU')
vi.mocked(getCurrentLocale).mockReturnValue('en-GB')
const hook2 = renderHook(() => useCurrentLanguage())

expect(currentLanguage()).toBe('en-US')
expect(hook2.result()).toBe('en-GB')
})
})
8 changes: 4 additions & 4 deletions src/utils/get-current-locale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ describe('getCurrentLocale', () => {

expect(getCurrentLocale()).toBe('en-US')

getUILanguage.mockReturnValue('ru-RU')
getUILanguage.mockReturnValue('en-GB')

expect(getCurrentLocale()).toBe('ru-RU')
expect(getCurrentLocale()).toBe('en-GB')
})

it('returns navigator language otherwise', () => {
const navigatorLanguage = vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')

expect(getCurrentLocale()).toBe('en-US')

navigatorLanguage.mockReturnValue('ru-RU')
navigatorLanguage.mockReturnValue('en-GB')

expect(getCurrentLocale()).toBe('ru-RU')
expect(getCurrentLocale()).toBe('en-GB')
})
})
4 changes: 2 additions & 2 deletions src/utils/get-document-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ describe('getDocumentLanguage', () => {
})

it('uses current locale when locale argument is not provided', () => {
vi.mocked(getCurrentLocale).mockReturnValue('ru-RU')
vi.mocked(getCurrentLocale).mockReturnValue('en-GB')

expect(getDocumentLanguage()).toBe('ru')
expect(getDocumentLanguage()).toBe('en')
})
})
8 changes: 4 additions & 4 deletions src/utils/intl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('getDateFormatter', () => {
it('memoizes instance by locales', () => {
expect(intl.getDateFormatter('en-US'))
.to.equal(intl.getDateFormatter(['en-US']))
.and.not.equal(intl.getDateFormatter('ru-RU'))
.and.not.equal(intl.getDateFormatter('en-GB'))
})

it('memoizes instance by options', () => {
expect(intl.getDateFormatter('en-US', { hour: '2-digit' }))
.to.equal(intl.getDateFormatter('en-US', { hour: '2-digit' }))
.and.not.equal(intl.getDateFormatter('en-US', { hour: 'numeric' }))
.and.not.equal(intl.getDateFormatter('ru-RU', { hour: '2-digit' }))
.and.not.equal(intl.getDateFormatter('en-GB', { hour: '2-digit' }))
})
})

Expand All @@ -35,13 +35,13 @@ describe('getNumberFormatter', () => {
it('memoizes instance by locales', () => {
expect(intl.getNumberFormatter('en-US'))
.to.equal(intl.getNumberFormatter(['en-US']))
.and.not.equal(intl.getNumberFormatter('ru-RU'))
.and.not.equal(intl.getNumberFormatter('en-GB'))
})

it('memoizes instance by options', () => {
expect(intl.getNumberFormatter('en-US', { maximumFractionDigits: 2 }))
.to.equal(intl.getNumberFormatter('en-US', { maximumFractionDigits: 2 }))
.and.not.equal(intl.getNumberFormatter('en-US', { maximumFractionDigits: 1 }))
.and.not.equal(intl.getNumberFormatter('ru-RU', { maximumFractionDigits: 2 }))
.and.not.equal(intl.getNumberFormatter('en-GB', { maximumFractionDigits: 2 }))
})
})

0 comments on commit 6c709d0

Please sign in to comment.