Skip to content

Commit

Permalink
chore: fix unit tests to support mocking of Link as function componen…
Browse files Browse the repository at this point in the history
…t (since found >= 1.0)
  • Loading branch information
Joona Ojapalo committed Aug 30, 2022
1 parent e582f77 commit 605adce
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
11 changes: 10 additions & 1 deletion test/unit/component/AboutPage.test.js
Original file line number Diff line number Diff line change
@@ -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('<AboutPage />', () => {
// setup Link component mock
const { Component: AboutPage } = proxyquire(
'../../../app/component/AboutPage',
{
'found/Link': MockLink,
},
);

const context = {
config: {
aboutThisService: {
Expand Down
11 changes: 10 additions & 1 deletion test/unit/component/RouteStop.test.js
Original file line number Diff line number Diff line change
@@ -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('<RouteStop />', () => {
// 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,
Expand Down
11 changes: 10 additions & 1 deletion test/unit/component/TripRouteStop.test.js
Original file line number Diff line number Diff line change
@@ -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('<TripRouteStop />', () => {
// 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,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/helpers/MockLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MockLink = ({ children }) => children;
export const MockQueryRenderer = ({ render }) => render();
6 changes: 1 addition & 5 deletions test/unit/helpers/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 605adce

Please sign in to comment.