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

Commit

Permalink
Update uuid mock to spyOn for v1 (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroshk authored Aug 30, 2023
1 parent 1f6f984 commit 506e3e8
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 96 deletions.
1 change: 1 addition & 0 deletions packages/terra-application/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* Changed
* Updated `ApplicationBase` documentaion.
* Locked `uuid` dependency to `3.4.0`.

## 1.55.0 - (February 17, 2023)

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 @@ -77,7 +77,7 @@
"terra-theme-provider": "^4.0.0",
"terra-toolbar": "^1.22.0",
"terra-visually-hidden-text": "^2.31.0",
"uuid": "^3.0.0",
"uuid": "3.4.0",
"wicg-inert": "3.1.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef, useContext, useLayoutEffect } from 'react';
import PropTypes from 'prop-types';
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';

import ApplicationLoadingOverlayContext from './ApplicationLoadingOverlayContext';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, {
useRef, useCallback, Suspense,
} from 'react';
import PropTypes from 'prop-types';
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';
import TerraApplicationNavigation from './private/ApplicationNavigation';
import {
titleConfigPropType, navigationItemsPropType, extensionItemsPropType, utilityItemsPropType, userConfigPropType, userActionConfigPropType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useRef, useContext, useLayoutEffect } from 'react';
import PropTypes from 'prop-types';
import Button from 'terra-button';
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';
import ApplicationStatusOverlayContext from './ApplicationStatusOverlayContext';

const propTypes = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';

import BannerRegistrationContext from './private/BannerRegistrationContext';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';

function getOverflowDataForElement(overflowElement) {
let overflowId = overflowElement.getAttribute('data-persistent-overflow-id');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,29 @@
import React from 'react';
import uuidv4 from 'uuid/v4';
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/v4');
const mockUuid = '00000000-0000-0000-0000-000000000000';
const loadingOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

describe('ApplicationLoadingOverlay', () => {
let reactUseContext;
let loadingOverlayContextValue;
let mockSpyUuid;
let mockSpyContext;

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(mockUuid);
});

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 +39,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(mockUuid);
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(mockUuid);
});

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(mockUuid);
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(mockUuid);
});

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(mockUuid);
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(mockUuid);
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(mockUuid);
});

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
@@ -1,40 +1,26 @@
import React from 'react';
import uuidv4 from 'uuid/v4';
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/v4');
const mockUuid = '00000000-0000-0000-0000-000000000000';
const statusOverlayContextValue = {
show: jest.fn(),
hide: jest.fn(),
};

describe('ApplicationStatusOverlay', () => {
let reactUseContext;
let statusOverlayContextValue;
let mockSpyUuid;
let mockSpyContext;

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(mockUuid);
});

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 +31,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(mockUuid);
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(mockUuid);
});

it('should render status view with the specified data', () => {
Expand All @@ -77,14 +63,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(mockUuid);
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(mockUuid);
});

it('should redisplay status view with new props', () => {
Expand All @@ -95,20 +81,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(mockUuid);
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(mockUuid);
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(mockUuid);
});

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

jest.mock('uuid/v4', () => () => 'test-uuid');
const testUuid = '00000000-0000-0000-0000-000000000000';

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

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

test('should apply overflow-id if none exists on element', () => {
const mockElement = {
getAttribute: () => undefined,
Expand All @@ -13,9 +23,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', testUuid);
expect(result[testUuid].scrollTop).toBe(15);
expect(result[testUuid].scrollLeft).toBe(20);
});

test('should not apply overflow-id if one exists on element', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/terra-dev-site/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* Changed
* Reverted `@mdx-js/loader`,`@mdx-js/mdx` and `@mdx-js/react` version to ^1.
* Locked `uuid` dependency to `3.4.0`.

## 7.8.0 - (February 17, 2023)

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 @@ -82,7 +82,7 @@
"terra-mixins": "^1.33.0",
"terra-search-field": "^3.13.0",
"terra-status-view": "^4.10.0",
"uuid": "^3.0.0"
"uuid": "3.4.0"
},
"peerDependencies": {
"react": "^16.8.5",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uuidv4 from 'uuid/v4';
import { v4 as uuidv4 } from 'uuid';

function getOverflowDataForElement(overflowElement) {
let overflowId = overflowElement.getAttribute('data-persistent-overflow-id');
Expand Down

0 comments on commit 506e3e8

Please sign in to comment.