Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
jest.mock replaced with spyOn (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroshk authored Aug 30, 2023
1 parent 8899894 commit daa888a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/terra-application/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Changed
* Updated jest snapshots for terra-icon and terra-button changes.
* Updated size explanations for ModalManager managed by DisclosureManagerContext.
* Updated `uuid` to `8.2.0` for consistency with other components.
* Locked `uuid` dependency to `3.4.0`.

* Added
* Added user action utility button.
Expand Down
2 changes: 1 addition & 1 deletion packages/terra-application/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"terra-theme-provider": "^4.0.0",
"terra-toolbar": "^1.22.0",
"terra-visually-hidden-text": "^2.31.0",
"uuid": "8.2.0",
"uuid": "3.4.0",
"wicg-inert": "3.1.2"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,26 @@ import React from 'react';
import { v4 as uuidv4 } from 'uuid';

import ApplicationLoadingOverlay from '../../../src/application-loading-overlay/ApplicationLoadingOverlay';
import ApplicationLoadingOverlayContext from '../../../src/application-loading-overlay/ApplicationLoadingOverlayContext';

jest.mock('uuid');

describe('ApplicationLoadingOverlay', () => {
let reactUseContext;
let loadingOverlayContextValue;
let mockSpyUuid;
let mockSpyContext;
const loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

beforeAll(() => {
uuidv4.mockReturnValue('test-id');

/**
* Until Enzyme is updated with full support for hooks, we need to
* mock out the useContext implementation.
*/
reactUseContext = React.useContext;
React.useContext = (contextValue) => {
if (ApplicationLoadingOverlayContext === contextValue) {
return loadingOverlayContextValue;
}
return reactUseContext(contextValue);
};
mockSpyContext = jest.spyOn(React, 'useContext').mockReturnValue(loadingOverlayContextValue);
mockSpyUuid = jest.spyOn(uuidv4, 'v4').mockReturnValue('00000000-0000-0000-0000-000000000000');
});

afterAll(() => {
React.useContext = reactUseContext;
mockSpyContext.mockRestore();
mockSpyUuid.mockRestore();
});

it('should render loading overlay as closed', () => {
loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

const wrapper = mount(
<ApplicationLoadingOverlay />,
);
Expand All @@ -51,80 +37,60 @@ describe('ApplicationLoadingOverlay', () => {
});

it('should render loading overlay as open', () => {
loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

const wrapper = mount(
<ApplicationLoadingOverlay isOpen />,
);

expect(wrapper).toMatchSnapshot();

expect(loadingOverlayContextValue.show.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.unmount();
expect(loadingOverlayContextValue.show.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should transition from open to closed', () => {
loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

const wrapper = mount(
<ApplicationLoadingOverlay isOpen />,
);

expect(wrapper).toMatchSnapshot();

expect(loadingOverlayContextValue.show.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.setProps({ isOpen: false });

expect(loadingOverlayContextValue.show.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(2);
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should redisplay loading overlay with new props', () => {
loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

const wrapper = mount(
<ApplicationLoadingOverlay isOpen />,
);

expect(wrapper).toMatchSnapshot();

expect(loadingOverlayContextValue.show.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.setProps({ backgroundStyle: 'dark' });

expect(loadingOverlayContextValue.show.mock.calls.length).toBe(2);
expect(loadingOverlayContextValue.show.mock.calls[1][0]).toBe('test-id');
expect(loadingOverlayContextValue.show.mock.calls[1][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(loadingOverlayContextValue.hide.mock.calls.length).toBe(1);
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(loadingOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should honor backgroundStyle prop', () => {
loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

const wrapper = mount(
<ApplicationLoadingOverlay isOpen backgroundStyle="clear" />,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,23 @@ import React from 'react';
import { v4 as uuidv4 } from 'uuid';

import ApplicationStatusOverlay from '../../../src/application-status-overlay/ApplicationStatusOverlay';
import ApplicationStatusOverlayContext from '../../../src/application-status-overlay/ApplicationStatusOverlayContext';

jest.mock('uuid');

describe('ApplicationStatusOverlay', () => {
let reactUseContext;
let statusOverlayContextValue;
let mockSpyUuid;
let mockSpyContext;
const statusOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

beforeAll(() => {
uuidv4.mockReturnValue('test-id');

/**
* Until Enzyme is updated with full support for hooks, we need to
* mock out the useContext implementation.
*/
reactUseContext = React.useContext;
React.useContext = (contextValue) => {
if (contextValue === ApplicationStatusOverlayContext) {
return statusOverlayContextValue;
}
return reactUseContext(contextValue);
};
mockSpyContext = jest.spyOn(React, 'useContext').mockReturnValue(statusOverlayContextValue);
mockSpyUuid = jest.spyOn(uuidv4, 'v4').mockReturnValue('00000000-0000-0000-0000-000000000000');
});

afterAll(() => {
React.useContext = reactUseContext;
});

beforeEach(() => {
statusOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};
mockSpyContext.mockRestore();
mockSpyUuid.mockRestore();
});

it('should render status view without any data', () => {
Expand All @@ -45,14 +29,14 @@ describe('ApplicationStatusOverlay', () => {
expect(wrapper).toMatchSnapshot();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.unmount();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should render status view with the specified data', () => {
Expand All @@ -77,14 +61,14 @@ describe('ApplicationStatusOverlay', () => {
expect(wrapper).toMatchSnapshot();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.unmount();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should redisplay status view with new props', () => {
Expand All @@ -95,20 +79,20 @@ describe('ApplicationStatusOverlay', () => {
expect(wrapper).toMatchSnapshot();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.show.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.setProps({ message: 'No data status view', variant: 'no-data' });

expect(statusOverlayContextValue.show.mock.calls.length).toBe(2);
expect(statusOverlayContextValue.show.mock.calls[1][0]).toBe('test-id');
expect(statusOverlayContextValue.show.mock.calls[1][0]).toBe('00000000-0000-0000-0000-000000000000');
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(0);

wrapper.unmount();

expect(statusOverlayContextValue.show.mock.calls.length).toBe(2);
expect(statusOverlayContextValue.hide.mock.calls.length).toBe(1);
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('test-id');
expect(statusOverlayContextValue.hide.mock.calls[0][0]).toBe('00000000-0000-0000-0000-000000000000');
});

it('should honor buttonAttrs prop', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { v4 as uuidv4 } from 'uuid';
import * as ScrollPersistence from '../../../../src/utils/scroll-persistence/scroll-persistence';

jest.mock('uuid', () => ({ v4: () => 'test-uuid' }));

describe('getOverflowDataForElement', () => {
let mockSpyUuid;
beforeAll(() => {
mockSpyUuid = jest.spyOn(uuidv4, 'v4').mockReturnValue('00000000-0000-0000-0000-000000000000');
});

afterAll(() => {
mockSpyUuid.mockRestore();
});

test('should apply overflow-id if none exists on element', () => {
const mockElement = {
getAttribute: () => undefined,
Expand All @@ -13,9 +21,9 @@ describe('getOverflowDataForElement', () => {

const result = ScrollPersistence.getOverflowDataForElement(mockElement);

expect(mockElement.setAttribute).toHaveBeenCalledWith('data-persistent-overflow-id', 'test-uuid');
expect(result['test-uuid'].scrollTop).toBe(15);
expect(result['test-uuid'].scrollLeft).toBe(20);
expect(mockElement.setAttribute).toHaveBeenCalledWith('data-persistent-overflow-id', '00000000-0000-0000-0000-000000000000');
expect(result['00000000-0000-0000-0000-000000000000'].scrollTop).toBe(15);
expect(result['00000000-0000-0000-0000-000000000000'].scrollLeft).toBe(20);
});

test('should not apply overflow-id if one exists on element', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/terra-dev-site/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Changed
* Updated jest snapshot for terra-icon and terra-button changes.
* Updated wdio snapshot to fix build.
* Updated `uuid` to `8.2.0` for consistency with other components.
* Locked `uuid` dependency to `3.4.0`.

## 8.1.0 - (June 22, 2022)

Expand Down
2 changes: 1 addition & 1 deletion packages/terra-dev-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"terra-mixins": "^1.33.0",
"terra-search-field": "^3.13.0",
"terra-status-view": "^4.10.0",
"uuid": "8.2.0"
"uuid": "3.4.0"
},
"peerDependencies": {
"react": "^16.8.5",
Expand Down

0 comments on commit daa888a

Please sign in to comment.