import React from 'react';
import { ThemeProvider, lightTheme } from '@strapi/parts';
import { render as renderTL, fireEvent, waitFor, screen } from '@testing-library/react';
import { QueryClientProvider, QueryClient } from 'react-query';
import { Provider } from 'react-redux';
import { combineReducers, createStore } from 'redux';
import en from '../../../translations/en.json';
import server from './server';
import reducers from '../../../hooks/reducers';
import LocaleSelect from '..';
jest.mock('../../../utils', () => ({
getTrad: x => x,
}));
jest.mock('react-intl', () => ({
FormattedMessage: ({ id }) => id,
useIntl: () => ({ formatMessage: jest.fn(({ id }) => en[id]) }),
}));
jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'),
useNotification: jest.fn(),
}));
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
},
},
});
const store = createStore(combineReducers(reducers));
const render = props =>
renderTL(
,
{ container: document.body }
);
describe('LocaleSelect', () => {
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
it('shows an aria-busy element and a loader when loading the data', async () => {
const { container } = render();
expect(container.firstChild).toMatchInlineSnapshot(`
.c3 {
position: absolute;
left: 0;
right: 0;
bottom: 0;
top: 0;
width: 100%;
background: transparent;
border: none;
}
.c3:focus {
outline: none;
}
.c1 {
font-weight: 500;
font-size: 0.75rem;
line-height: 1.33;
color: #32324d;
}
.c12 {
font-weight: 400;
font-size: 0.875rem;
line-height: 1.43;
color: #666687;
}
.c7 {
padding-left: 12px;
}
.c11 {
padding-right: 16px;
padding-left: 16px;
}
.c4 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.c6 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-align-items: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.c0 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.c0 > * {
margin-top: 0;
margin-bottom: 0;
}
.c0 > * + * {
margin-top: 4px;
}
.c2 {
position: relative;
border: 1px solid #dcdce4;
padding-right: 12px;
border-radius: 4px;
background: #ffffff;
overflow: hidden;
}
.c2:focus-within {
border: 1px solid #4945ff;
}
.c13 {
background: transparent;
border: none;
position: relative;
z-index: 1;
}
.c13 svg {
height: 0.6875rem;
width: 0.6875rem;
}
.c13 svg path {
fill: #666687;
}
.c14 {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
background: none;
border: none;
}
.c14 svg {
width: 0.375rem;
}
.c5 {
min-height: 2.5rem;
}
.c9 {
border: 0;
-webkit-clip: rect(0 0 0 0);
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
.c10 {
-webkit-animation: gzYjWD 1s infinite linear;
animation: gzYjWD 1s infinite linear;
}
.c8 img {
height: 1rem;
width: 1rem;
}
Locales
Loading the available locales...
`);
});
it('only shows the locales that have not already been used', async () => {
render();
await waitFor(() =>
expect(screen.queryByText('Loading the available locales...')).not.toBeInTheDocument()
);
fireEvent.mouseDown(screen.getByLabelText('Locales'));
await waitFor(() => screen.getByRole('listbox'));
expect(screen.getByText('Afrikaans (af)')).toBeVisible();
expect(screen.getByText('French (fr)')).toBeVisible();
expect(screen.queryByText('English (en)')).toBeFalsy();
});
it('brings back an object of code and displayName keys when changing', async () => {
const onLocaleChangeSpy = jest.fn();
render({ onLocaleChange: onLocaleChangeSpy });
await waitFor(() =>
expect(screen.queryByText('Loading the available locales...')).not.toBeInTheDocument()
);
fireEvent.mouseDown(screen.getByLabelText('Locales'));
await waitFor(() => screen.getByRole('listbox'));
fireEvent.click(screen.getByText('French (fr)'));
expect(onLocaleChangeSpy).toBeCalledWith({ code: 'fr', displayName: 'French (fr)' });
});
});