Skip to content

Commit

Permalink
[PFX-444] - Convert react intl tests to jest (#646)
Browse files Browse the repository at this point in the history
* tests: converted utils to jest

* tests: with-intil-provider to jest

* tests: convert with-locale-required to jest

* tests: convert locale-requied to jest

* tests: convert use-locale-required to jest

* tests: convert next.test to jest

* tests: refactor
  • Loading branch information
jpina1-godaddy authored Jan 5, 2024
1 parent de5967f commit 252cbf9
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 279 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/gasket-react-intl/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
collectCoverageFrom: ['lib/**/*.js'],
setupFiles: ['<rootDir>/test/setup.js'],
testEnvironment: 'jsdom'
};
6 changes: 4 additions & 2 deletions packages/gasket-react-intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "npm run test:runner",
"test": "jest",
"test:runner": "mocha --require setup-env --require test/setup.js \"test/*.test.js\"",
"test:watch": "npm run test:runner -- --watch",
"test:coverage": "nyc --reporter=text --reporter=json-summary npm run test:runner",
Expand Down Expand Up @@ -73,6 +73,7 @@
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-unicorn": "^44.0.0",
"intl": "^1.2.5",
"jest": "^29.3.1",
"jsdom": "^20.0.0",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
Expand All @@ -92,7 +93,8 @@
},
"eslintConfig": {
"extends": [
"godaddy-react"
"godaddy-react",
"plugin:jest/recommended"
],
"plugins": [
"unicorn"
Expand Down
58 changes: 22 additions & 36 deletions packages/gasket-react-intl/test/locale-required.test.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,60 @@
import React from 'react';
import assume from 'assume';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import { render } from '@testing-library/react';
import mockManifest from './fixtures/mock-manifest.json';
import { LocaleStatus } from '../src/utils';
import LocaleRequired from '../src/locale-required';
import useLocaleRequired from '../src/use-locale-required';

jest.mock('../src/use-locale-required');

const { ERROR, LOADED, LOADING } = LocaleStatus;

describe('LocaleRequired', function () {
let mockConfig, useLocaleRequiredStub, LocaleRequired, wrapper;
let wrapper;

const doMount = props => <LocaleRequired { ...props }>MockComponent</LocaleRequired>;

beforeEach(function () {
useLocaleRequiredStub = sinon.stub();
mockConfig = {
defaultLocale: 'en-US',
manifest: { ...mockManifest, paths: { ...mockManifest.paths } },
isBrowser: false
};
LocaleRequired = proxyquire('../src/locale-required', {
'./config': mockConfig,
'./use-locale-required': {
default: useLocaleRequiredStub
}
}).default;
});

afterEach(function () {
sinon.restore();
useLocaleRequired.mockClear();
});

describe('#render', function () {
it('renders null if loading', function () {
useLocaleRequiredStub.returns(LOADING);
useLocaleRequired.mockReturnValue(LOADING);
wrapper = render(doMount({}));
assume(wrapper.baseElement.textContent).eqls('');
expect(wrapper.baseElement.textContent).toEqual('');
});

it('renders custom loader if loading', function () {
useLocaleRequiredStub.returns(LOADING);
useLocaleRequired.mockReturnValue(LOADING);
wrapper = render(doMount({ loading: 'loading...' }));
assume(wrapper.baseElement.textContent).eqls('loading...');
expect(wrapper.baseElement.textContent).toEqual('loading...');
});

it('renders wrapped component if LOADED', function () {
useLocaleRequiredStub.returns(LOADED);
useLocaleRequired.mockReturnValue(LOADED);
wrapper = render(doMount({ loading: 'loading...' }));
assume(wrapper.baseElement.textContent).includes('MockComponent');
expect(wrapper.baseElement.textContent).toContain('MockComponent');
});

it('renders wrapped component if ERROR', function () {
useLocaleRequiredStub.returns(ERROR);
useLocaleRequired.mockReturnValue(ERROR);
wrapper = render(doMount({ loading: 'loading...' }));
assume(wrapper.baseElement.textContent).includes('MockComponent');
expect(wrapper.baseElement.textContent).toContain('MockComponent');
});

it('supports localesPath', function () {
useLocaleRequiredStub.returns(LOADING);
useLocaleRequired.mockReturnValue(LOADING);
wrapper = render(doMount({ localesPath: '/bogus' }));
assume(useLocaleRequiredStub).calledWith('/bogus');
assume(wrapper.baseElement.textContent).eqls('');
expect(useLocaleRequired).toHaveBeenCalledWith('/bogus');
expect(wrapper.baseElement.textContent).toEqual('');
});

it('supports localesPath as thunk', function () {
const mockThunk = sinon.stub();
useLocaleRequiredStub.returns(LOADING);
const mockThunk = jest.fn();
useLocaleRequired.mockReturnValue(LOADING);
wrapper = render(doMount({ localesPath: mockThunk }));
assume(useLocaleRequiredStub).calledWith(mockThunk);
assume(wrapper.baseElement.textContent).eqls('');
expect(useLocaleRequired).toHaveBeenCalledWith(mockThunk);
expect(wrapper.baseElement.textContent).toEqual('');
});
});
});
69 changes: 32 additions & 37 deletions packages/gasket-react-intl/test/next.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable no-console */
import assume from 'assume';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import { jest } from '@jest/globals';

describe('Next.js functions', function () {
let next, res;

beforeEach(function () {
sinon.stub(console, 'error');
jest.spyOn(console, 'error').mockImplementation(() => {});
res = {
locals: {
gasketData: {
Expand All @@ -17,29 +15,22 @@ describe('Next.js functions', function () {
}
}
};
next = proxyquire('../src/next', {
['./config']: {
manifest: {
defaultPath: '/locales',
defaultLocale: 'en-US'
}
}
});
next = require('../src/next');
});

afterEach(function () {
sinon.restore();
jest.restoreAllMocks();
});

describe('intlGetStaticProps', function () {
it('makes getStaticProps function', function () {
const results = next.intlGetStaticProps();
assume(results).instanceOf(Function);
expect(typeof results).toBe('function');
});

it('returns localesProps for default path', async function () {
const results = await next.intlGetStaticProps()({ params: { locale: 'en-US' } });
assume(results).eqls({
const results = await next.intlGetStaticProps('/locales')({ params: { locale: 'en-US' } });
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -52,7 +43,7 @@ describe('Next.js functions', function () {

it('returns localesProps for other path part', async function () {
const results = await next.intlGetStaticProps('/locales/extra')({ params: { locale: 'en-US' } });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -65,11 +56,11 @@ describe('Next.js functions', function () {

it('handles thunks for locale path part', async function () {
const mockContext = { params: { locale: 'en-US' }, extra: true };
const mockThunk = sinon.stub().callsFake((context) => context.extra ? '/locales/extra' : '/locales');
const mockThunk = jest.fn().mockImplementation((context) => context.extra ? '/locales/extra' : '/locales');

const results = await next.intlGetStaticProps(mockThunk)(mockContext);
assume(mockThunk).calledWith(mockContext);
assume(results).eqls({
expect(mockThunk).toHaveBeenCalledWith(mockContext);
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -82,7 +73,7 @@ describe('Next.js functions', function () {

it('returns localesProps for multiple locale path parts', async function () {
const results = await next.intlGetStaticProps(['/locales', '/locales/extra'])({ params: { locale: 'en-US' } });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -97,8 +88,9 @@ describe('Next.js functions', function () {
});

it('returns localesProps with error for missing path', async function () {
const consoleSpy = jest.spyOn(console, 'error');
const results = await next.intlGetStaticProps('/locales/missing')({ params: { locale: 'en-US' } });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -107,12 +99,13 @@ describe('Next.js functions', function () {
}
}
});
assume(console.error).is.calledWithMatch('Cannot find module');
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Cannot find module'));
consoleSpy.mockRestore();
});

it('returns localesProps for default if locale missing', async function () {
const results = await next.intlGetStaticProps('/locales')({ params: { locale: 'fr-CA' } });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'fr-CA',
Expand All @@ -125,7 +118,7 @@ describe('Next.js functions', function () {

it('uses locale on context from builtin i18n routing', async function () {
const results = await next.intlGetStaticProps('/locales')({ locale: 'fr-CA' });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'fr-CA',
Expand All @@ -140,12 +133,12 @@ describe('Next.js functions', function () {
describe('intlGetServerSideProps', function () {
it('makes getServerSideProps function', function () {
const results = next.intlGetServerSideProps();
assume(results).instanceOf(Function);
expect(typeof results).toBe('function');
});

it('returns localesProps for default path', async function () {
const results = await next.intlGetServerSideProps()({ res });
assume(results).eqls({
const results = await next.intlGetServerSideProps('/locales')({ res });
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -158,7 +151,7 @@ describe('Next.js functions', function () {

it('returns localesProps for other path part', async function () {
const results = await next.intlGetServerSideProps('/locales/extra')({ res });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -171,11 +164,11 @@ describe('Next.js functions', function () {

it('handles thunks for locale path part', async function () {
const mockContext = { res, extra: true };
const mockThunk = sinon.stub().callsFake((context) => context.extra ? '/locales/extra' : '/locales');
const mockThunk = jest.fn().mockImplementation((context) => context.extra ? '/locales/extra' : '/locales');

const results = await next.intlGetServerSideProps(mockThunk)(mockContext);
assume(mockThunk).calledWith(mockContext);
assume(results).eqls({
expect(mockThunk).toHaveBeenCalledWith(mockContext);
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -188,7 +181,7 @@ describe('Next.js functions', function () {

it('returns localesProps for multiple locale path parts', async function () {
const results = await next.intlGetServerSideProps(['/locales', '/locales/extra'])({ res });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -203,8 +196,9 @@ describe('Next.js functions', function () {
});

it('returns localesProps with error for missing path', async function () {
const consoleSpy = jest.spyOn(console, 'error');
const results = await next.intlGetServerSideProps('/locales/missing')({ res });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'en-US',
Expand All @@ -213,13 +207,14 @@ describe('Next.js functions', function () {
}
}
});
assume(console.error).is.calledWithMatch('Cannot find module');
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Cannot find module'));
consoleSpy.mockRestore();
});

it('returns localesProps for default if locale missing', async function () {
res.locals.gasketData.intl.locale = 'fr-CA';
const results = await next.intlGetServerSideProps('/locales')({ res });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'fr-CA',
Expand All @@ -232,7 +227,7 @@ describe('Next.js functions', function () {

it('uses locale on context from builtin i18n routing', async function () {
const results = await next.intlGetServerSideProps('/locales')({ locale: 'fr-CA', res });
assume(results).eqls({
expect(results).toEqual({
props: {
localesProps: {
locale: 'fr-CA',
Expand Down
Loading

0 comments on commit 252cbf9

Please sign in to comment.