Skip to content

Commit

Permalink
tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jpina1-godaddy committed Jan 4, 2024
1 parent 2a1a642 commit 323dc4d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
18 changes: 9 additions & 9 deletions packages/gasket-react-intl/test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ jest.mock('../src/config', () => ({
manifest: require('./fixtures/mock-manifest.json')
}));

describe('utils', () => {
describe('utils', function () {
let mockConfig;

beforeEach(() => {
beforeEach(function () {
mockConfig = require('../src/config');
});

afterEach(() => {
afterEach(function () {
jest.restoreAllMocks();
delete global.window.gasketIntlLocale;
delete global.navigator.languages;
});

it('exports localeUtils instance', () => {
it('exports localeUtils instance', function () {
expect(utils).toHaveProperty('localeUtils');
expect(utils.localeUtils).toBeInstanceOf(LocaleUtils);
});

describe('getActiveLocale', () => {
it('returns defaultLocale from manifest', () => {
describe('getActiveLocale', function () {
it('returns defaultLocale from manifest', function () {
const results = utils.getActiveLocale();
expect(results).toEqual(mockConfig.manifest.defaultLocale);
});

it('returns locale from window property', () => {
it('returns locale from window property', function () {
mockConfig.isBrowser = true;
mockConfig.clientData.locale = 'fr';
global.window.gasketIntlLocale = 'de';
Expand All @@ -43,7 +43,7 @@ describe('utils', () => {
expect(results).toEqual('de');
});

it('returns locale from clientData', () => {
it('returns locale from clientData', function () {
mockConfig.isBrowser = true;
mockConfig.clientData.locale = 'fr';
Object.defineProperty(global.navigator, 'languages', {
Expand All @@ -54,7 +54,7 @@ describe('utils', () => {
expect(results).toEqual('fr');
});

it('returns locale from first navigator.languages', () => {
it('returns locale from first navigator.languages', function () {
mockConfig.isBrowser = true;
mockConfig.clientData.locale = null;
Object.defineProperty(global.navigator, 'languages', {
Expand Down
28 changes: 14 additions & 14 deletions packages/gasket-react-intl/test/with-intl-provider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ const MockComponent = class extends React.Component {
}
};

describe('withIntlProvider', () => {
describe('withIntlProvider', function () {
let mockConfig;
let withIntlProvider;

beforeEach(() => {
beforeEach(function () {
mockConfig = require('../src/config');
withIntlProvider = withIntlProviderDefault();
});

it('adds display name', () => {
it('adds display name', function () {
const WrappedComponent = withIntlProvider(MockComponent);
expect(WrappedComponent.displayName).toBe('withIntlProvider(MockComponent)');
});

it('hoists non-react statics', () => {
it('hoists non-react statics', function () {
const WrappedComponent = withIntlProvider(MockComponent);
expect(WrappedComponent).not.toHaveProperty('bogus');
MockComponent.bogus = 'BOGUS';
Expand All @@ -32,7 +32,7 @@ describe('withIntlProvider', () => {
delete MockComponent.bogus;
});

it('hoists getInitialProps if set', () => {
it('hoists getInitialProps if set', function () {
const WrappedComponent = withIntlProvider(MockComponent);
expect(WrappedComponent).not.toHaveProperty('getInitialProps');
MockComponent.getInitialProps = f => f;
Expand All @@ -41,17 +41,17 @@ describe('withIntlProvider', () => {
delete MockComponent.getInitialProps;
});

describe('reducer', () => {
describe('reducer', function () {
let initState;

beforeEach(() => {
beforeEach(function () {
initState = {
messages: { en: { first: 'First' } },
status: { '/locales/en/first.json': LOADED }
};
});

it('LOADED actions add messages and file status', () => {
it('LOADED actions add messages and file status', function () {
const action = { type: LOADED, payload: { locale: 'en', messages: { example: 'Example' }, file: '/locales/en.json' } };
const result = reducer(initState, action);
expect(result).toEqual({
Expand All @@ -68,7 +68,7 @@ describe('withIntlProvider', () => {
});
});

it('ERROR actions add file status', () => {
it('ERROR actions add file status', function () {
const action = { type: ERROR, payload: { file: '/locales/en.json' } };
const result = reducer(initState, action);
expect(result).toEqual({
Expand All @@ -84,7 +84,7 @@ describe('withIntlProvider', () => {
});
});

it('LOADING actions add file status', () => {
it('LOADING actions add file status', function () {
const action = { type: LOADING, payload: { file: '/locales/en.json' } };
const result = reducer(initState, action);
expect(result).toEqual({
Expand All @@ -101,16 +101,16 @@ describe('withIntlProvider', () => {
});
});

describe('init', () => {
it('initializes state with empty objects', () => {
describe('init', function () {
it('initializes state with empty objects', function () {
const result = init({});
expect(result).toEqual({
messages: {},
status: {}
});
});

it('initializes state with locales props', () => {
it('initializes state with locales props', function () {
const result = init({
locale: 'en',
messages: { en: { example: 'Example' } },
Expand All @@ -122,7 +122,7 @@ describe('withIntlProvider', () => {
});
});

it('merges locales props data with client data', () => {
it('merges locales props data with client data', function () {
mockConfig.clientData = {
locale: 'en',
messages: { en: { first: 'First' } },
Expand Down
40 changes: 20 additions & 20 deletions packages/gasket-react-intl/test/with-locale-required.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,33 @@ const MockComponent = class extends React.Component {
}
};

describe('withLocaleRequired', () => {
describe('withLocaleRequired', function () {
let useLocaleRequiredMock, wrapper;

const doMount = (...args) => {
const Wrapped = withLocaleRequired(...args)(MockComponent);
return render(<Wrapped />);
};

beforeEach(() => {
beforeEach(function () {
useLocaleRequiredMock = require('../src/use-locale-required');
});

afterEach(() => {
afterEach(function () {
jest.restoreAllMocks();
});

it('adds display name', () => {
it('adds display name', function () {
const wrapped = withLocaleRequired()(MockComponent);
expect(wrapped.displayName).toBe('withLocaleRequired(MockComponent)');
});

it('adds display name with ForwardRef', () => {
it('adds display name with ForwardRef', function () {
const wrapped = withLocaleRequired('/locales', { forwardRef: true })(MockComponent);
expect(wrapped.displayName).toBe('ForwardRef(withLocaleRequired/MockComponent))');
});

it('hoists non-react statics', () => {
it('hoists non-react statics', function () {
const wrapped = withLocaleRequired()(MockComponent);
expect(wrapped).not.toHaveProperty('bogus');
MockComponent.bogus = 'BOGUS';
Expand All @@ -50,25 +50,25 @@ describe('withLocaleRequired', () => {
delete MockComponent.bogus;
});

describe('#getInitialProps', () => {
afterEach(() => {
describe('#getInitialProps', function () {
afterEach(function () {
delete MockComponent.getInitialProps;
});

it('hoists getInitialProps if set', () => {
it('hoists getInitialProps if set', function () {
const wrapped = withLocaleRequired()(MockComponent);
expect(wrapped).not.toHaveProperty('getInitialProps');
MockComponent.getInitialProps = jest.fn();
const wrappedGetInitialProps = withLocaleRequired()(MockComponent);
expect(wrappedGetInitialProps).toHaveProperty('getInitialProps');
});

it('adds getInitialProps if initialProps set', () => {
it('adds getInitialProps if initialProps set', function () {
const wrapped = withLocaleRequired('/locales', { initialProps: true })(MockComponent);
expect(wrapped).toHaveProperty('getInitialProps');
});

it('executes wrapped getInitialProps', async () => {
it('executes wrapped getInitialProps', async function () {
MockComponent.getInitialProps = jest.fn().mockResolvedValue({ bogus: true });
const wrapped = withLocaleRequired('/locales', { initialProps: true })(MockComponent);
const ctx = {};
Expand All @@ -77,7 +77,7 @@ describe('withLocaleRequired', () => {
expect(props).toEqual({ bogus: true });
});

it('loads localeProps on server', async () => {
it('loads localeProps on server', async function () {
MockComponent.getInitialProps = jest.fn().mockResolvedValue({ bogus: true });
const wrapped = withLocaleRequired('/locales', { initialProps: true })(MockComponent);
const ctx = {
Expand All @@ -104,7 +104,7 @@ describe('withLocaleRequired', () => {
});
});

it('handles missing gasketData', async () => {
it('handles missing gasketData', async function () {
MockComponent.getInitialProps = jest.fn().mockResolvedValue({ bogus: true });
const wrapped = withLocaleRequired('/locales', { initialProps: true })(MockComponent);
const ctx = {
Expand All @@ -123,7 +123,7 @@ describe('withLocaleRequired', () => {
expect(error).toBe(null);
});

it('resolve localePahThunk and passes as prop', async () => {
it('resolve localePahThunk and passes as prop', async function () {
const mockThunk = jest.fn().mockImplementation((context) => (context.extra ? '/locales/extra' : '/locales'));
MockComponent.getInitialProps = jest.fn().mockResolvedValue({ bogus: true });
const wrapped = withLocaleRequired(mockThunk, { initialProps: true })(MockComponent);
Expand Down Expand Up @@ -154,35 +154,35 @@ describe('withLocaleRequired', () => {
});
});

describe('#render', () => {
it('renders empty if loading', () => {
describe('#render', function () {
it('renders empty if loading', function () {
useLocaleRequiredMock.mockReturnValue(LOADING); // Set the mock return value to LOADING;
wrapper = doMount();
expect(wrapper.baseElement.innerHTML).toEqual('<div></div>');
});

it('renders custom loader if loading', async () => {
it('renders custom loader if loading', async function () {
useLocaleRequiredMock.mockReturnValue(LOADING);
doMount('/locales', { loading: loadingText });
const textEl = await screen.findByText(loadingText);
expect(textEl.innerHTML).toEqual(loadingText);
});

it('renders wrapped component if LOADED', async () => {
it('renders wrapped component if LOADED', async function () {
useLocaleRequiredMock.mockReturnValue(LOADED);
doMount({ loading: loadingText });
const textEl = await screen.findByText('MockComponent');
expect(textEl.innerHTML).toEqual('MockComponent');
});

it('renders wrapped component if ERROR', async () => {
it('renders wrapped component if ERROR', async function () {
useLocaleRequiredMock.mockReturnValue(ERROR);
doMount({ loading: loadingText });
const textEl = await screen.findByText('MockComponent');
expect(textEl.innerHTML).toEqual('MockComponent');
});

it('forwards refs', async () => {
it('forwards refs', async function () {
class TestComponent extends React.Component {
// Used to test ref forwarding
getMockData() {
Expand Down

0 comments on commit 323dc4d

Please sign in to comment.