From 605adce3d0ddf2d6f6af66050e65c80a0e532d0d Mon Sep 17 00:00:00 2001 From: Joona Ojapalo Date: Tue, 30 Aug 2022 11:31:41 +0300 Subject: [PATCH] chore: fix unit tests to support mocking of Link as function component (since found >= 1.0) --- test/unit/component/AboutPage.test.js | 11 ++++++++++- test/unit/component/RouteStop.test.js | 11 ++++++++++- test/unit/component/TripRouteStop.test.js | 11 ++++++++++- test/unit/helpers/MockLink.js | 2 ++ test/unit/helpers/init.js | 6 +----- 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 test/unit/helpers/MockLink.js diff --git a/test/unit/component/AboutPage.test.js b/test/unit/component/AboutPage.test.js index 7e166d18e6..e05f81cdea 100644 --- a/test/unit/component/AboutPage.test.js +++ b/test/unit/component/AboutPage.test.js @@ -1,9 +1,18 @@ import React from 'react'; -import { Component as AboutPage } from '../../../app/component/AboutPage'; +import proxyquire from 'proxyquire'; import { mountWithIntl } from '../helpers/mock-intl-enzyme'; import { mockChildContextTypes } from '../helpers/mock-context'; +import { MockLink } from '../helpers/MockLink'; describe('', () => { + // setup Link component mock + const { Component: AboutPage } = proxyquire( + '../../../app/component/AboutPage', + { + 'found/Link': MockLink, + }, + ); + const context = { config: { aboutThisService: { diff --git a/test/unit/component/RouteStop.test.js b/test/unit/component/RouteStop.test.js index a25e6b33a7..044cd7fa7f 100644 --- a/test/unit/component/RouteStop.test.js +++ b/test/unit/component/RouteStop.test.js @@ -1,11 +1,20 @@ import React from 'react'; +import proxyquire from 'proxyquire'; import { mountWithIntl } from '../helpers/mock-intl-enzyme'; -import RouteStop from '../../../app/component/RouteStop'; import ServiceAlertIcon from '../../../app/component/ServiceAlertIcon'; import { AlertSeverityLevelType } from '../../../app/constants'; +import { MockLink } from '../helpers/MockLink'; describe('', () => { + // setup Link component mock + const { default: RouteStop } = proxyquire( + '../../../app/component/RouteStop', + { + 'found/Link': MockLink, + }, + ); + it('should not render a service alert icon for the stop', () => { const props = { currentTime: 1471515614, diff --git a/test/unit/component/TripRouteStop.test.js b/test/unit/component/TripRouteStop.test.js index dd7667b724..9e38f8baed 100644 --- a/test/unit/component/TripRouteStop.test.js +++ b/test/unit/component/TripRouteStop.test.js @@ -1,12 +1,21 @@ import React from 'react'; import PropTypes from 'prop-types'; +import proxyquire from 'proxyquire'; import { mountWithIntl } from '../helpers/mock-intl-enzyme'; -import TripRouteStop from '../../../app/component/TripRouteStop'; import ServiceAlertIcon from '../../../app/component/ServiceAlertIcon'; import { AlertSeverityLevelType } from '../../../app/constants'; +import { MockLink } from '../helpers/MockLink'; describe('', () => { + // setup Link component mock + const { default: TripRouteStop } = proxyquire( + '../../../app/component/TripRouteStop', + { + 'found/Link': MockLink, + }, + ); + it('should not render a service alert icon for the trip route stop if the alert is not active', () => { const props = { currentTime: 1471515614, diff --git a/test/unit/helpers/MockLink.js b/test/unit/helpers/MockLink.js new file mode 100644 index 0000000000..1cbfbde369 --- /dev/null +++ b/test/unit/helpers/MockLink.js @@ -0,0 +1,2 @@ +export const MockLink = ({ children }) => children; +export const MockQueryRenderer = ({ render }) => render(); diff --git a/test/unit/helpers/init.js b/test/unit/helpers/init.js index 3aac4a58be..b3d635fe8f 100644 --- a/test/unit/helpers/init.js +++ b/test/unit/helpers/init.js @@ -2,12 +2,12 @@ import { expect } from 'chai'; import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16'; -import Link from 'found/Link'; import * as relay from 'react-relay'; import { JSDOM } from 'jsdom'; import { after, afterEach, before } from 'mocha'; import { stub } from 'sinon'; import { initAnalyticsClientSide } from '../../../app/util/analyticsUtils'; +import { MockQueryRenderer } from './MockLink'; /** * Helper function to copy the properties of the source object to the @@ -55,16 +55,12 @@ const noop = () => null; require.extensions['.png'] = noop; require.extensions['.svg'] = noop; -const MockLink = ({ children }) => children; -const MockQueryRenderer = ({ render }) => render(); - // set up mocha hooks before('setting up the environment', () => { const callback = warning => { throw new Error(warning); }; stub(console, 'error').callsFake(callback); - stub(Link, 'render').value(MockLink); stub(relay, 'QueryRenderer').value(MockQueryRenderer); // TODO this could be renabled when dependencies don't throw warnings // stub(console, 'warn').callsFake(callback);