From 0cdfe97ab97c06c1b141d187e97cbe2dc1b63e9d Mon Sep 17 00:00:00 2001 From: Filip Satek Date: Wed, 28 Aug 2024 17:08:34 +0200 Subject: [PATCH] test: migrate to IMA Testing Library --- .gitignore | 1 + jest.config.itl.js | 20 ++ .../jestSetup.js => jestSetup.itl.js | 0 package.json | 3 - packages/plugin-atoms/jest.config.js | 19 +- .../__tests__/__snapshots__/mountSpec.js.snap | 5 - .../plugin-atoms/src/__tests__/mountSpec.js | 107 ------ .../src/components/__tests__/IframeSpec.jsx | 38 +++ .../__snapshots__/IframeSpec.jsx.snap | 25 ++ packages/plugin-select/jest.config.js | 9 +- packages/plugin-select/jestSetupFile.js | 11 - .../src/select/__tests__/SelectSpec.js | 197 ++++++----- .../__snapshots__/SelectSpec.js.snap | 322 +++++++----------- .../EnzymePageRenderer.js | 2 + 14 files changed, 312 insertions(+), 447 deletions(-) create mode 100644 jest.config.itl.js rename packages/plugin-atoms/jestSetup.js => jestSetup.itl.js (100%) delete mode 100644 packages/plugin-atoms/src/__tests__/__snapshots__/mountSpec.js.snap delete mode 100644 packages/plugin-atoms/src/__tests__/mountSpec.js create mode 100644 packages/plugin-atoms/src/components/__tests__/IframeSpec.jsx create mode 100644 packages/plugin-atoms/src/components/__tests__/__snapshots__/IframeSpec.jsx.snap delete mode 100644 packages/plugin-select/jestSetupFile.js diff --git a/.gitignore b/.gitignore index 56ffb98f..15adfe11 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ .eslintcache *.tgz **/.turbo +.swc/ !itl.tgz diff --git a/jest.config.itl.js b/jest.config.itl.js new file mode 100644 index 00000000..306261f0 --- /dev/null +++ b/jest.config.itl.js @@ -0,0 +1,20 @@ +const { + setImaTestingLibraryServerConfig, + FALLBACK_APP_MAIN_PATH, + FALLBACK_APPLICATION_FOLDER, +} = require('@ima/testing-library/server'); + +const base = require('./jest.config.base.js'); + +setImaTestingLibraryServerConfig({ + applicationFolder: FALLBACK_APPLICATION_FOLDER, +}); + +module.exports = { + ...base, + preset: '@ima/testing-library', + moduleNameMapper: { + 'app/main': FALLBACK_APP_MAIN_PATH, + }, + setupFiles: ['/../../jestSetup.itl.js'], +}; diff --git a/packages/plugin-atoms/jestSetup.js b/jestSetup.itl.js similarity index 100% rename from packages/plugin-atoms/jestSetup.js rename to jestSetup.itl.js diff --git a/package.json b/package.json index 57c3f18d..e97374e7 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "devDependencies": { "@babel/eslint-parser": "^7.21.8", "@babel/preset-react": "^7.22.3", - "@cfaester/enzyme-adapter-react-18": "^0.7.0", "@changesets/cli": "^2.26.1", "@ima/plugin-cli": "^19.1.2", "@ima/testing-library": "itl.tgz", @@ -46,8 +45,6 @@ "@types/react-dom": "^18.3.0", "@typescript-eslint/eslint-plugin": "^5.59.8", "@typescript-eslint/parser": "^5.59.8", - "enzyme": "3.11.0", - "enzyme-to-json": "^3.6.2", "eslint": "^8.41.0", "eslint-config-prettier": "^8.8.0", "eslint-plugin-import": "^2.27.5", diff --git a/packages/plugin-atoms/jest.config.js b/packages/plugin-atoms/jest.config.js index 834a51b2..3dd3a9d3 100644 --- a/packages/plugin-atoms/jest.config.js +++ b/packages/plugin-atoms/jest.config.js @@ -1,20 +1,5 @@ -const { - setImaTestingLibraryServerConfig, - FALLBACK_APP_MAIN_PATH, - FALLBACK_APPLICATION_FOLDER, -} = require('@ima/testing-library/server'); - -const base = require('../../jest.config.base.js'); - -setImaTestingLibraryServerConfig({ - applicationFolder: FALLBACK_APPLICATION_FOLDER, -}); +const itl = require('../../jest.config.itl.js'); module.exports = { - ...base, - preset: '@ima/testing-library', - moduleNameMapper: { - 'app/main': FALLBACK_APP_MAIN_PATH, - }, - setupFiles: ['./jestSetup.js'], + ...itl, }; diff --git a/packages/plugin-atoms/src/__tests__/__snapshots__/mountSpec.js.snap b/packages/plugin-atoms/src/__tests__/__snapshots__/mountSpec.js.snap deleted file mode 100644 index 92892587..00000000 --- a/packages/plugin-atoms/src/__tests__/__snapshots__/mountSpec.js.snap +++ /dev/null @@ -1,5 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`UIAtoms mount rendering should render atoms without noscript tag: should render Iframe without noscript tag 1`] = `"
"`; - -exports[`UIAtoms mount rendering should render atoms without noscript tag: should render Image without noscript tag 1`] = `""`; diff --git a/packages/plugin-atoms/src/__tests__/mountSpec.js b/packages/plugin-atoms/src/__tests__/mountSpec.js deleted file mode 100644 index 556cd841..00000000 --- a/packages/plugin-atoms/src/__tests__/mountSpec.js +++ /dev/null @@ -1,107 +0,0 @@ -import { PageContext } from '@ima/react-page-renderer'; -import classnames from 'classnames'; -import { mount } from 'enzyme'; -import { Infinite } from 'infinite-circle'; -import { JSDOM } from 'jsdom'; -// eslint-disable-next-line no-unused-vars -import { useState } from 'react'; -import { toMockedInstance } from 'to-mock'; - -import _router from './mocks/router'; -import _settings from './mocks/settings'; -import _window from './mocks/window'; -import ComponentPositions from '../ComponentPositions'; -import * as UIAtoms from '../main'; -import UIComponentHelper from '../UIComponentHelper'; -// eslint-disable-next-line import/order -import Visibility from '../Visibility'; - -jest.mock('react', () => ({ - ...jest.requireActual('react'), - useState: () => [true, () => {}], -})); - -const jsdom = new JSDOM(''); -const { window } = jsdom; - -global.window = window; -global.document = window.document; -global.navigator = { - userAgent: 'node.js', -}; - -const visibility = toMockedInstance(Visibility); -const mockPosition = { - height: 0, -}; -const componentPositions = toMockedInstance(ComponentPositions, { - getWindowViewportRect: () => mockPosition, -}); -const infinite = toMockedInstance(Infinite); - -function getComponentOptions(overrideSettings = {}) { - const $Settings = Object.assign({}, _settings, overrideSettings); - - const $UIComponentHelper = new UIComponentHelper( - _router, - _window, - componentPositions, - visibility, - infinite, - classnames - ); - - const context = { - $Utils: { - $Settings, - $UIComponentHelper, - $CssClasses: classnames, - }, - }; - const mountOptions = { - context, - someProp: true, - }; - - return mountOptions; -} - -describe('UIAtoms mount rendering', () => { - let wrapper = null; - - afterEach(() => { - if (wrapper) { - wrapper.unmount(); - } - }); - - describe('should render atoms without noscript tag: ', () => { - let mountOptions = getComponentOptions(); - - it('should render Image without noscript tag', () => { - const Component = UIAtoms.Image; - wrapper = mount( - - - , - mountOptions - ); - - expect(wrapper.html()).toMatchSnapshot(); - expect(wrapper.find('noscript')).toHaveLength(0); - }); - - it('should render Iframe without noscript tag', () => { - const Component = UIAtoms.Iframe; - wrapper = mount( - - - , - mountOptions - ); - - expect(wrapper.html()).toMatchSnapshot(); - expect(wrapper.find('noscript')).toHaveLength(0); - }); - }); -}); diff --git a/packages/plugin-atoms/src/components/__tests__/IframeSpec.jsx b/packages/plugin-atoms/src/components/__tests__/IframeSpec.jsx new file mode 100644 index 00000000..05877b44 --- /dev/null +++ b/packages/plugin-atoms/src/components/__tests__/IframeSpec.jsx @@ -0,0 +1,38 @@ +import { renderWithContext, waitFor } from '@ima/testing-library'; + +import { Iframe, Visibility } from '../../main'; + +describe('Iframe', () => { + it('should render with noscript tag by default', async () => { + const { container } = await renderWithContext( +