From 506e3e837a993f06f08e98c12640a7d6ab72595f Mon Sep 17 00:00:00 2001 From: Alla Doroshkevych Date: Wed, 30 Aug 2023 15:19:28 -0400 Subject: [PATCH] Update uuid mock to spyOn for v1 (#350) --- packages/terra-application/CHANGELOG.md | 1 + packages/terra-application/package.json | 2 +- .../ApplicationLoadingOverlay.jsx | 2 +- .../ApplicationNavigation.jsx | 2 +- .../ApplicationStatusOverlay.jsx | 2 +- .../NotificationBanner.jsx | 2 +- .../scroll-persistence/scroll-persistence.js | 2 +- .../ApplicationLoadingOverlay.test.jsx | 70 +++++-------------- .../ApplicationStatusOverlay.test.jsx | 52 +++++--------- .../scroll-persistence.test.js | 18 +++-- packages/terra-dev-site/CHANGELOG.md | 1 + packages/terra-dev-site/package.json | 2 +- .../scroll-persistence/scroll-persistence.js | 2 +- 13 files changed, 62 insertions(+), 96 deletions(-) diff --git a/packages/terra-application/CHANGELOG.md b/packages/terra-application/CHANGELOG.md index 57a161dfb..2af6662e5 100644 --- a/packages/terra-application/CHANGELOG.md +++ b/packages/terra-application/CHANGELOG.md @@ -6,6 +6,7 @@ * Changed * Updated `ApplicationBase` documentaion. + * Locked `uuid` dependency to `3.4.0`. ## 1.55.0 - (February 17, 2023) diff --git a/packages/terra-application/package.json b/packages/terra-application/package.json index 566847b77..66bc1f87f 100644 --- a/packages/terra-application/package.json +++ b/packages/terra-application/package.json @@ -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": { diff --git a/packages/terra-application/src/application-loading-overlay/ApplicationLoadingOverlay.jsx b/packages/terra-application/src/application-loading-overlay/ApplicationLoadingOverlay.jsx index 9fac2ef8b..dd1de6b02 100644 --- a/packages/terra-application/src/application-loading-overlay/ApplicationLoadingOverlay.jsx +++ b/packages/terra-application/src/application-loading-overlay/ApplicationLoadingOverlay.jsx @@ -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'; diff --git a/packages/terra-application/src/application-navigation/ApplicationNavigation.jsx b/packages/terra-application/src/application-navigation/ApplicationNavigation.jsx index 7306b7a79..be9f8ddc2 100644 --- a/packages/terra-application/src/application-navigation/ApplicationNavigation.jsx +++ b/packages/terra-application/src/application-navigation/ApplicationNavigation.jsx @@ -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, diff --git a/packages/terra-application/src/application-status-overlay/ApplicationStatusOverlay.jsx b/packages/terra-application/src/application-status-overlay/ApplicationStatusOverlay.jsx index 49daf4eb7..3819e0833 100644 --- a/packages/terra-application/src/application-status-overlay/ApplicationStatusOverlay.jsx +++ b/packages/terra-application/src/application-status-overlay/ApplicationStatusOverlay.jsx @@ -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 = { diff --git a/packages/terra-application/src/notification-banner/NotificationBanner.jsx b/packages/terra-application/src/notification-banner/NotificationBanner.jsx index f1df70885..be4d7f815 100644 --- a/packages/terra-application/src/notification-banner/NotificationBanner.jsx +++ b/packages/terra-application/src/notification-banner/NotificationBanner.jsx @@ -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'; diff --git a/packages/terra-application/src/utils/scroll-persistence/scroll-persistence.js b/packages/terra-application/src/utils/scroll-persistence/scroll-persistence.js index 9c3dc44af..77d04c36a 100644 --- a/packages/terra-application/src/utils/scroll-persistence/scroll-persistence.js +++ b/packages/terra-application/src/utils/scroll-persistence/scroll-persistence.js @@ -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'); diff --git a/packages/terra-application/tests/jest/application-loading-overlay/ApplicationLoadingOverlay.test.jsx b/packages/terra-application/tests/jest/application-loading-overlay/ApplicationLoadingOverlay.test.jsx index fd68b836b..d153be1f4 100644 --- a/packages/terra-application/tests/jest/application-loading-overlay/ApplicationLoadingOverlay.test.jsx +++ b/packages/terra-application/tests/jest/application-loading-overlay/ApplicationLoadingOverlay.test.jsx @@ -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( , ); @@ -51,11 +39,6 @@ describe('ApplicationLoadingOverlay', () => { }); it('should render loading overlay as open', () => { - loadingOverlayContextValue = { - show: jest.fn(), - hide: jest.fn(), - }; - const wrapper = mount( , ); @@ -63,21 +46,16 @@ describe('ApplicationLoadingOverlay', () => { 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( , ); @@ -85,22 +63,17 @@ describe('ApplicationLoadingOverlay', () => { 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( , ); @@ -108,23 +81,18 @@ describe('ApplicationLoadingOverlay', () => { 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( , ); diff --git a/packages/terra-application/tests/jest/application-status-overlay/ApplicationStatusOverlay.test.jsx b/packages/terra-application/tests/jest/application-status-overlay/ApplicationStatusOverlay.test.jsx index d21503f8c..b3593b922 100644 --- a/packages/terra-application/tests/jest/application-status-overlay/ApplicationStatusOverlay.test.jsx +++ b/packages/terra-application/tests/jest/application-status-overlay/ApplicationStatusOverlay.test.jsx @@ -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', () => { @@ -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', () => { @@ -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', () => { @@ -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', () => { diff --git a/packages/terra-application/tests/jest/utils/scroll-persistence/scroll-persistence.test.js b/packages/terra-application/tests/jest/utils/scroll-persistence/scroll-persistence.test.js index 0e5dc082e..4bb08ae4a 100644 --- a/packages/terra-application/tests/jest/utils/scroll-persistence/scroll-persistence.test.js +++ b/packages/terra-application/tests/jest/utils/scroll-persistence/scroll-persistence.test.js @@ -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, @@ -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', () => { diff --git a/packages/terra-dev-site/CHANGELOG.md b/packages/terra-dev-site/CHANGELOG.md index d4dc4e52d..533c5eaf5 100644 --- a/packages/terra-dev-site/CHANGELOG.md +++ b/packages/terra-dev-site/CHANGELOG.md @@ -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) diff --git a/packages/terra-dev-site/package.json b/packages/terra-dev-site/package.json index 279e7e4b3..7a4310fd0 100644 --- a/packages/terra-dev-site/package.json +++ b/packages/terra-dev-site/package.json @@ -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", diff --git a/packages/terra-dev-site/src/utils/scroll-persistence/scroll-persistence.js b/packages/terra-dev-site/src/utils/scroll-persistence/scroll-persistence.js index 99c9713db..424a9d42a 100644 --- a/packages/terra-dev-site/src/utils/scroll-persistence/scroll-persistence.js +++ b/packages/terra-dev-site/src/utils/scroll-persistence/scroll-persistence.js @@ -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');