Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Jest for JS unit testing #437

Merged
merged 65 commits into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from 59 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
652cbd7
Remove isFrontendIframeLoaded function
swissspidy Aug 22, 2019
c865bb4
Add initial Jest config
swissspidy Aug 22, 2019
1430b66
Port over some initial tests
swissspidy Aug 22, 2019
c5d6d33
Setup globals for jest unit tests
swissspidy Aug 22, 2019
820971c
Fix moduleNameMapper
swissspidy Aug 22, 2019
44dbe10
Use proper module imports
swissspidy Aug 22, 2019
712f1d5
Make readableLargeNumber always return a string
swissspidy Aug 22, 2019
183457a
Update test globals
swissspidy Aug 22, 2019
8dbfc2a
Update some tests
swissspidy Aug 22, 2019
d428649
Remove qunit
swissspidy Aug 22, 2019
601afd6
Further update tests
swissspidy Aug 22, 2019
bf58866
Fix import of wp.sanitize
swissspidy Aug 26, 2019
f2f0cc1
Add back babel/polyfill
swissspidy Sep 17, 2019
d0eaf6e
Port data api tests over to jest
swissspidy Sep 25, 2019
e0729a6
fix: Fix fillFilterWithComponent test
tofumatt Oct 15, 2019
803b34c
fix: Test getQuery params with DI
tofumatt Oct 15, 2019
b398da2
fix: Package-lock tweaks
tofumatt Oct 15, 2019
b17c7ca
fix: Reauth test
tofumatt Oct 16, 2019
677acdd
fix: Include Intl to fix numberFormat tests
tofumatt Oct 16, 2019
e702b09
chore: Make tests easier to read
tofumatt Oct 16, 2019
6ea6c73
fix: Fix toggle test
tofumatt Oct 16, 2019
e6e8898
chore: fix lint
tofumatt Oct 28, 2019
e53d8c1
update npm to latest before installing
aaemnnosttv Oct 29, 2019
480011b
fix: Use Node 10 (on Travis and in general)
tofumatt Oct 29, 2019
26623ad
Merge branch 'add/jest-for-unit-testing' of github.com:google/site-ki…
tofumatt Oct 29, 2019
b3e532f
chore: Remove npm latest install on Travis
tofumatt Oct 29, 2019
0e872d5
fix: Use NVM
tofumatt Oct 29, 2019
8effec7
fix: Remove nvm use
tofumatt Oct 29, 2019
10e16b7
fix: Do not import stripTags
tofumatt Oct 29, 2019
11dd715
chore: Merge develop
tofumatt Oct 29, 2019
5a7b14d
fix: case issues
tofumatt Oct 29, 2019
6a4d29d
fix: More test fixes
tofumatt Oct 29, 2019
a8f9bfc
fix: Run test only once
tofumatt Oct 30, 2019
e5d9b2d
chore: Fix package issues
tofumatt Oct 30, 2019
1934d4d
fix: Get webpack working with Jest and E2E
tofumatt Oct 31, 2019
3491f09
try clean install with npm
aaemnnosttv Oct 31, 2019
9995f71
pin backstop at 3.8.8 for now
aaemnnosttv Oct 31, 2019
8b3407c
fix: Remove gulp task
tofumatt Nov 1, 2019
c3d975f
fix: Don't use eslint-disable
tofumatt Nov 1, 2019
b8f4029
fix: More gulp fixes
tofumatt Nov 1, 2019
c090629
fix: Make test:js the run-once option
tofumatt Nov 1, 2019
37d49cd
fix: Lodash import placement
tofumatt Nov 1, 2019
4071ba9
chore: Fix JSDoc
tofumatt Nov 1, 2019
a140f7b
Merge branch 'develop' into add/jest-for-unit-testing
tofumatt Nov 1, 2019
22c4aab
fix: Gulp lint
tofumatt Nov 1, 2019
02edea3
fix: Remove ununused import/export
tofumatt Nov 4, 2019
c57ffd2
fix: Caps
tofumatt Nov 4, 2019
53fceb3
chore: Make `wp` global usage fail lint
tofumatt Nov 4, 2019
601b91d
Merge branch 'develop' into add/jest-for-unit-testing
tofumatt Nov 4, 2019
451b8f4
feat: Add React-features shim to repo
tofumatt Nov 4, 2019
a058d2f
Merge branch 'add/jest-for-unit-testing' of github.com:google/site-ki…
tofumatt Nov 4, 2019
9abc947
fix: Never use WP global
tofumatt Nov 4, 2019
5333506
fix: APIFetch imports
tofumatt Nov 4, 2019
cfffa24
try: Use TerserPlugin (#782)
tofumatt Nov 4, 2019
ae11dd7
fix: Bad rename
tofumatt Nov 6, 2019
6d32ce9
fix: webpack config for storybook
tofumatt Nov 6, 2019
c53f7e6
fix: Maintain translation functions
tofumatt Nov 6, 2019
22382cb
fix: Add all i18n functions
tofumatt Nov 6, 2019
cd8af25
fix: Move QUnit test to Jest
tofumatt Nov 6, 2019
14f7ef0
fix: Remove unused setup file
tofumatt Nov 6, 2019
ccfeee5
fix: Use react-features
tofumatt Nov 6, 2019
78ef379
fix: Remove API Key usage
tofumatt Nov 6, 2019
76835ca
fix: Use defaultProfileID
tofumatt Nov 6, 2019
9f24287
fix: Place lodash in correct import section
tofumatt Nov 6, 2019
ab5e45b
fix: revert defaultProfileId
tofumatt Nov 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc.json
Expand Up @@ -21,8 +21,7 @@
"lodash": "readonly",
"googlesitekitCurrentModule": "readonly",
"gtag": "readonly",
"process": "readonly",
"wp": "readonly"
tofumatt marked this conversation as resolved.
Show resolved Hide resolved
"process": "readonly"
},
"rules": {
"valid-jsdoc": "warn"
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
@@ -0,0 +1 @@
10
6 changes: 3 additions & 3 deletions .storybook/config.js
Expand Up @@ -49,7 +49,7 @@ import { googlesitekit as dashboardData } from '../.storybook/data/wp-admin-admi
const googlesitekit = dashboardData;

// Setup.
window.wp = window.wp || {};
const wp = {};
wp.element = wp.element || {};
wp.components = wp.components || {};
wp.i18n = wp.i18n || {};
Expand All @@ -71,10 +71,10 @@ wp.element.Fragment = Fragment;
wp.element.createElement = createElement;
wp.element.createPortal = createPortal;
wp.components.withFilters = withFilters;
window.lodash = lodash;
wp.i18n.__ = __ || {};
wp.i18n.setLocaleData = setLocaleData || {};
wp.i18n.sprintf = sprintf || {};
window.wp = window.wp || wp;
window.React = React;
window.lodash = lodash;
window.googlesitekit = window.googlesitekit || googlesitekit;
Expand All @@ -83,7 +83,7 @@ window.googlesitekit.admin = window.googlesitekit.admin || googlesitekit.admin;
window.googlesitekit.modules = window.googlesitekit.modules || googlesitekit.modules;
window.googlesitekit.admin.assetsRoot = '/assets/';
window.googlesitekit.isStorybook = true;
wp.apiFetch = ( vars ) => {
window.wp.apiFetch = ( vars ) => {
const matches = vars.path.match( '/google-site-kit/v1/modules/(.*)/data/(.*[^/])' );

if ( window.googlesitekit.modules[ matches[ 1 ] ][ matches[ 2 ] ] ) {
Expand Down
4 changes: 4 additions & 0 deletions .storybook/webpack.config.js
Expand Up @@ -19,5 +19,9 @@ module.exports = async ( { config } ) => {
}
);

config.externals = {
'@wordpress/api-fetch': [ 'wp', 'apiFetch' ],
};

return config;
};
5 changes: 3 additions & 2 deletions .travis.yml
Expand Up @@ -38,7 +38,8 @@ branches:

# Before install, failures in this section will result in build status 'errored'
before_install:
- npm install || exit 1
- nvm install
- npm ci
- docker run --rm -v "$PWD:/app" composer install
- |
if [[ "$PHP" == "1" ]] || [[ "$JS" == "1" ]] || [[ "$SNIFF" == "1" ]]; then
Expand Down Expand Up @@ -68,7 +69,7 @@ script:
- |
if [[ "$JS" == "1" ]]; then
npm run build || exit 1 # Build for tests.
npx gulp qunit || exit 1 # QUnit tests
npm run test:js || exit 1 # JS unit tests
npm run test || exit 1 # Bundle size test
travis_retry npm run travis:visualtest || exit 1 # Visual regression tests
fi
Expand Down
7 changes: 5 additions & 2 deletions assets/js/components/adminbar/adminbar-modules.js
Expand Up @@ -16,8 +16,11 @@
* limitations under the License.
*/

const { withFilters } = wp.components;
const { Component, Fragment } = wp.element;
/**
* WordPress dependencies
*/
import { withFilters } from '@wordpress/components';
import { Component, Fragment } from '@wordpress/element';

class AdminbarModules extends Component {
render() {
Expand Down
7 changes: 5 additions & 2 deletions assets/js/components/alert.js
Expand Up @@ -23,8 +23,11 @@ import data from 'GoogleComponents/data';
import Notification from 'GoogleComponents/notifications/notification';
import PropTypes from 'prop-types';

const { __ } = wp.i18n;
const { Component, Fragment } = wp.element;
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';

class Alert extends Component {
constructor( props ) {
Expand Down
7 changes: 5 additions & 2 deletions assets/js/components/analytics-inactive-cta.js
Expand Up @@ -30,8 +30,11 @@ import CTA from 'GoogleComponents/notifications/cta';
import PropTypes from 'prop-types';
import GenericError from 'GoogleComponents/notifications/generic-error';

const { Component } = wp.element;
const { __ } = wp.i18n;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

class AnalyticsInactiveCTA extends Component {
static async setupAnalyticsClick() {
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/button.js
Expand Up @@ -22,7 +22,10 @@
import { MDCRipple } from 'SiteKitCore/material-components';
import PropTypes from 'prop-types';

const { Component, createRef } = wp.element;
/**
* WordPress dependencies
*/
import { Component, createRef } from '@wordpress/element';

class Button extends Component {
constructor( props ) {
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/checkbox.js
Expand Up @@ -22,7 +22,10 @@
import { MDCFormField, MDCCheckbox } from 'SiteKitCore/material-components';
import PropTypes from 'prop-types';

const { Component, createRef } = wp.element;
/**
* WordPress dependencies
*/
import { Component, createRef } from '@wordpress/element';

class Checkbox extends Component {
constructor( props ) {
Expand Down
Expand Up @@ -31,8 +31,11 @@ import { decodeHtmlEntity } from 'GoogleUtil';
import DateRangeSelector from 'GoogleComponents/date-range-selector';
import HelpLink from 'GoogleComponents/help-link';

const { Component, Fragment } = wp.element;
const { __ } = wp.i18n;
/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

class DashboardDetailsApp extends Component {
constructor( props ) {
Expand Down
Expand Up @@ -16,8 +16,11 @@
* limitations under the License.
*/

const { Component } = wp.element;
const { withFilters } = wp.components;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withFilters } from '@wordpress/components';

/**
* A single module. Keeps track of its own active state and settings.
Expand Down
Expand Up @@ -20,7 +20,10 @@
* Internal dependencies
*/
import DashboardDetailsModule from './dashboard-details-module';
const { Component } = wp.element;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class DashboardDetailsModules extends Component {
render() {
Expand Down
12 changes: 8 additions & 4 deletions assets/js/components/dashboard-splash/dashboard-splash-app.js
Expand Up @@ -22,6 +22,12 @@
import DashboardSplashMain from 'GoogleComponents/dashboard-splash/dashboard-splash-main';
import { Suspense as ReactSuspense, lazy as ReactLazy } from 'react';

/**
* WordPress dependencies
*/
import { Component, Fragment, Suspense as WPSuspense, lazy as WPlazy } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't we extract this logic to a react-features helper?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that ever got merged; at any rate it was weird because even the version of React we load depends on what Gutenberg/other scripts expose. I'll have a look though. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be revisited, it should import from util/react-features.

/**
* Internal dependencies
*/
Expand All @@ -31,13 +37,11 @@ import { sendAnalyticsTrackingEvent } from 'GoogleUtil';
import 'GoogleComponents/publisher-wins';
import 'GoogleComponents/notifications';

const { Component, Fragment } = wp.element;
let { Suspense, lazy } = wp.element;
const { __ } = wp.i18n;

// Check for `Suspense` and `lazy` in `wp.element`; versions before 2.4.0 did
// not include either, so we need to fallback to the React versions. See:
// https://github.com/WordPress/gutenberg/blob/master/packages/element/CHANGELOG.md#240-2019-05-21
let Suspense = WPSuspense;
let lazy = WPlazy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, this can be removed following the above change.

if ( ! Suspense ) {
Suspense = ReactSuspense;
}
Expand Down
Expand Up @@ -23,8 +23,11 @@ import Button from 'GoogleComponents/button';
import Logo from 'GoogleComponents/logo';
import PropTypes from 'prop-types';

const { Component } = wp.element;
const { __ } = wp.i18n;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

class DashboardSplashIntro extends Component {
render() {
Expand Down
Expand Up @@ -24,8 +24,11 @@ import DashboardSplashService from './dashboard-splash-service';
import DashboardSplashOutro from './dashboard-splash-outro';
import DashboardSplashModule from './dashboard-splash-module';

const { Component } = wp.element;
const { __ } = wp.i18n;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

class DashboardSplashMain extends Component {
constructor( props ) {
Expand Down
Expand Up @@ -22,7 +22,10 @@
import PropTypes from 'prop-types';
import SvgIcon from 'GoogleUtil/svg-icon';

const { Component } = wp.element;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class DashboardSplashModule extends Component {
render() {
Expand Down
Expand Up @@ -16,8 +16,11 @@
* limitations under the License.
*/

const { Component } = wp.element;
const { withFilters } = wp.components;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withFilters } from '@wordpress/components';

/**
* A single module. Keeps track of its own active state and settings.
Expand Down
Expand Up @@ -23,8 +23,11 @@ import SvgIcon from 'GoogleUtil/svg-icon';
import Button from 'GoogleComponents/button';
import PropTypes from 'prop-types';

const { Component } = wp.element;
const { __ } = wp.i18n;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

class DashboardSplashOutro extends Component {
render() {
Expand Down
Expand Up @@ -25,7 +25,10 @@ import PropTypes from 'prop-types';
*/
import Link from '../link';

const { Component } = wp.element;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class DashboardSplashService extends Component {
render() {
Expand Down
9 changes: 6 additions & 3 deletions assets/js/components/dashboard/dashboard-app.js
Expand Up @@ -25,15 +25,18 @@ import PageHeader from 'GoogleComponents/page-header';
import 'GoogleComponents/publisher-wins';
import 'GoogleComponents/notifications';

/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import DashboardMain from './dashboard-main';
import DashboardNotifications from './dashboard-notifications';

const { Component, Fragment } = wp.element;
const { __ } = wp.i18n;

class DashboardApp extends Component {
render() {
return (
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/dashboard/dashboard-main.js
Expand Up @@ -20,7 +20,10 @@
* Internal dependencies
*/
import DashboardModules from './dashboard-modules';
const { Component, Fragment } = wp.element;
/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';

class DashboardMain extends Component {
render() {
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/dashboard/dashboard-module-header.js
Expand Up @@ -21,7 +21,10 @@
*/
import PropTypes from 'prop-types';

const { Component } = wp.element;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class DashboardModuleHeader extends Component {
render() {
Expand Down
7 changes: 5 additions & 2 deletions assets/js/components/dashboard/dashboard-module.js
Expand Up @@ -16,8 +16,11 @@
* limitations under the License.
*/

const { Component } = wp.element;
const { withFilters } = wp.components;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withFilters } from '@wordpress/components';

/**
* A single module. Keeps track of its own active state and settings.
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/dashboard/dashboard-modules.js
Expand Up @@ -25,7 +25,10 @@ import DashboardModule from './dashboard-module';
*/
import HelpLink from 'GoogleComponents/help-link';
import { Fragment } from 'react';
const { Component } = wp.element;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

class DashboardModules extends Component {
render() {
Expand Down
7 changes: 5 additions & 2 deletions assets/js/components/dashboard/dashboard-notifications.js
Expand Up @@ -16,8 +16,11 @@
* limitations under the License.
*/

const { Component } = wp.element;
const { withFilters } = wp.components;
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withFilters } from '@wordpress/components';

/**
* A single module. Keeps track of its own active state and settings.
Expand Down
5 changes: 4 additions & 1 deletion assets/js/components/data-block.js
Expand Up @@ -23,7 +23,10 @@ import PropTypes from 'prop-types';
import SourceLink from 'GoogleComponents/source-link';
import SvgIcon from 'GoogleUtil/svg-icon';

const { Component, Fragment } = wp.element;
/**
* WordPress dependencies
*/
import { Component, Fragment } from '@wordpress/element';

class DataBlock extends Component {
constructor( props ) {
Expand Down