From e8360af6ce487989667ea66cee5d6fd41cb8fa58 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 14 Jan 2025 11:39:28 -0300 Subject: [PATCH] use actual store for test --- .../I18N/components/specs/I18NMenu.spec.tsx | 90 ++++++++++++------- 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/app/react/I18N/components/specs/I18NMenu.spec.tsx b/app/react/I18N/components/specs/I18NMenu.spec.tsx index 8a291a30f9..e6743238da 100644 --- a/app/react/I18N/components/specs/I18NMenu.spec.tsx +++ b/app/react/I18N/components/specs/I18NMenu.spec.tsx @@ -4,15 +4,29 @@ import React from 'react'; import { act, fireEvent, RenderResult, screen, render } from '@testing-library/react'; import { Location, MemoryRouter } from 'react-router-dom'; +import { createStore, Provider } from 'jotai'; import { ClientUserSchema } from 'app/apiResponseTypes'; import { inlineEditAtom, localeAtom, settingsAtom, userAtom } from 'V2/atoms'; import { TestAtomStoreProvider } from 'V2/testing'; import { UserRole } from 'shared/types/userSchema'; +import { LanguageISO6391 } from 'shared/types/commonTypes'; import { I18NMenu } from '../I18NMenu'; const defaultLanguages = [ - { _id: '1', label: 'English', key: 'en', localized_label: 'English', default: true }, - { _id: '2', label: 'Spanish', key: 'es', localized_label: 'Español', default: false }, + { + _id: '1', + label: 'English', + key: 'en' as LanguageISO6391, + localized_label: 'English', + default: true, + }, + { + _id: '2', + label: 'Spanish', + key: 'es' as LanguageISO6391, + localized_label: 'Español', + default: false, + }, ]; const users = [ @@ -91,34 +105,6 @@ describe('I18NMenu', () => { expect(renderResult.container).toMatchSnapshot('after turning on live translate'); }); - it('should trigger a reload if the current language is deleted', async () => { - renderComponent(users[0]); - - const newSettingsAtomValue = { - languages: [ - { _id: '2', label: 'Spanish', key: 'es', localized_label: 'Español', default: true }, - ], - }; - - renderResult.rerender( - - - - - - ); - - expect(window.location.assign).toHaveBeenCalledTimes(1); - expect(window.location.assign).toHaveBeenCalledWith('/es/library'); - }); - describe('when there is a user', () => { it('should render then laguages and the live translate option', () => { renderComponent(users[0]); @@ -167,4 +153,48 @@ describe('I18NMenu', () => { } ); }); + + describe('reloading after language change', () => { + const testStore = createStore(); + testStore.set(userAtom, users[0]); + testStore.set(localeAtom, 'en'); + testStore.set(settingsAtom, settingsAtomValue); + + it('should trigger a reload if the current language is deleted', async () => { + const result = render( + + + + + + ); + + const newSettingsAtomValue = { + languages: [ + { + _id: '2', + label: 'Spanish', + key: 'es' as LanguageISO6391, + localized_label: 'Español', + default: true, + }, + ], + }; + + await act(() => { + testStore.set(settingsAtom, newSettingsAtomValue); + }); + + result.rerender( + + + + + + ); + + expect(window.location.assign).toHaveBeenCalledTimes(1); + expect(window.location.assign).toHaveBeenCalledWith('/library'); + }); + }); });