Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

feat(one-app-router): update to 1.1.0 #137

Merged
merged 6 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 33 additions & 15 deletions __tests__/client/initClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jest.mock('react', () => {

jest.mock('@americanexpress/one-app-router', () => {
const reactRouter = require.requireActual('@americanexpress/one-app-router');
jest.spyOn(reactRouter, 'match');
jest.spyOn(reactRouter, 'matchPromise');
return reactRouter;
});

Expand Down Expand Up @@ -95,9 +95,12 @@ describe('initClient', () => {
window.location.replace = jest.fn();
const promiseResolveSpy = jest.spyOn(Promise, 'resolve');

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce(
(config, cb) => cb(null, { pathname: 'path/to/redirected/location' }, null)
const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(
() => Promise.resolve({
redirectLocation: { pathname: 'path/to/redirected/location' },
renderProps: null,
})
);

const { loadPrerenderScripts } = require('../../src/client/prerender');
Expand All @@ -115,11 +118,15 @@ describe('initClient', () => {
expect.assertions(1);
const promiseRejectionSpy = jest.spyOn(Promise, 'reject');

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce(
const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(
(config, cb) => cb('error', { pathname: 'path/to/redirected/location' }, { testProp: 'test' })
);

matchPromise.mockImplementationOnce(
() => Promise.reject(new Error('error'))
);

const { loadPrerenderScripts } = require('../../src/client/prerender');
loadPrerenderScripts.mockReturnValueOnce(Promise.resolve());

Expand All @@ -139,9 +146,11 @@ describe('initClient', () => {

document.getElementById = jest.fn(() => ({ remove: jest.fn() }));

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce((config, cb) => cb(null, null, { testProp: 'test' }));

const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(() => Promise.resolve({
redirectLocation: null,
renderProps: { testProp: 'test' },
}));
const { loadPrerenderScripts } = require('../../src/client/prerender');
loadPrerenderScripts.mockReturnValueOnce(Promise.resolve());
promiseResolveSpy.mockClear();
Expand All @@ -159,8 +168,11 @@ describe('initClient', () => {

document.getElementById = jest.fn(() => ({ remove: jest.fn() }));

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce((config, cb) => cb(null, null, { testProp: 'test' }));
const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(() => Promise.resolve({
redirectLocation: null,
renderProps: { testProp: 'test' },
}));

const { loadPrerenderScripts } = require('../../src/client/prerender');
loadPrerenderScripts.mockReturnValueOnce(Promise.resolve());
Expand All @@ -178,8 +190,11 @@ describe('initClient', () => {
expect.assertions(2);
document.getElementById = jest.fn(() => ({ remove: jest.fn() }));

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce((config, cb) => cb(null, null, { testProp: 'test' }));
const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(() => Promise.resolve({
redirectLocation: null,
renderProps: { testProp: 'test' },
}));

const { loadServiceWorker } = require('../../src/client/prerender');

Expand All @@ -201,8 +216,11 @@ describe('initClient', () => {
};
[...new Array(5)].forEach(() => document.body.appendChild(createStyle()));

const { match } = require('@americanexpress/one-app-router');
match.mockImplementationOnce((config, cb) => cb(null, null, { testProp: 'test' }));
const { matchPromise } = require('@americanexpress/one-app-router');
matchPromise.mockImplementationOnce(() => Promise.resolve({
redirectLocation: null,
renderProps: { testProp: 'test' },
}));

const { loadPrerenderScripts } = require('../../src/client/prerender');
loadPrerenderScripts.mockReturnValueOnce(Promise.resolve());
Expand Down
17 changes: 10 additions & 7 deletions __tests__/server/middleware/createRequestHtmlFragment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
*/

import url from 'url';
import { browserHistory } from '@americanexpress/one-app-router';
import { browserHistory, matchPromise } from '@americanexpress/one-app-router';
import { Map as iMap, fromJS } from 'immutable';
import { composeModules } from 'holocron';
import match from '../../../src/universal/utils/matchPromisified';
// getBreaker is only added in the mock
/* eslint-disable-next-line import/named */
import { getBreaker } from '../../../src/server/utils/createCircuitBreaker';

import * as reactRendering from '../../../src/server/utils/reactRendering';

jest.mock('../../../src/universal/utils/matchPromisified');
jest.mock('@americanexpress/one-app-router', () => {
const reactRouter = require.requireActual('@americanexpress/one-app-router');
jest.spyOn(reactRouter, 'matchPromise');
return reactRouter;
});

jest.mock('holocron', () => ({
composeModules: jest.fn(() => 'composeModules'),
Expand Down Expand Up @@ -61,7 +64,7 @@ describe('createRequestHtmlFragment', () => {
}));

beforeAll(() => {
match.mockImplementation(({ routes, location }) => Promise.resolve({
matchPromise.mockImplementation(({ routes, location }) => Promise.resolve({
redirectLocation: undefined,
renderProps: {
routes,
Expand Down Expand Up @@ -178,7 +181,7 @@ describe('createRequestHtmlFragment', () => {
'../../../src/server/middleware/createRequestHtmlFragment'
).default;

match.mockImplementationOnce(() => ({
matchPromise.mockImplementationOnce(() => ({
redirectLocation: undefined,
// omit renderProps
}));
Expand All @@ -203,7 +206,7 @@ describe('createRequestHtmlFragment', () => {
'../../../src/server/middleware/createRequestHtmlFragment'
).default;

match.mockImplementationOnce(() => ({
matchPromise.mockImplementationOnce(() => ({
redirectLocation: {
pathname: '/redirect',
search: '',
Expand All @@ -225,7 +228,7 @@ describe('createRequestHtmlFragment', () => {
'../../../src/server/middleware/createRequestHtmlFragment'
).default;

match.mockImplementationOnce(() => ({
matchPromise.mockImplementationOnce(() => ({
redirectLocation: {
state: url.parse('https://example.com/redirect'),
},
Expand Down
21 changes: 18 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"@americanexpress/lumberjack": "^1.1.3",
"@americanexpress/one-app-bundler": "^6.5.0",
"@americanexpress/one-app-ducks": "^4.0.1",
"@americanexpress/one-app-router": "^1.0.0",
"@americanexpress/one-app-router": "^1.1.0",
"@americanexpress/vitruvius": "^2.0.0",
"babel-preset-amex": "^3.2.0",
"body-parser": "^1.19.0",
Expand Down
6 changes: 2 additions & 4 deletions src/client/initClient.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
import React from 'react';
import { hydrate } from 'react-dom';
import { Provider } from 'react-redux';
import { browserHistory, Router } from '@americanexpress/one-app-router';
import { browserHistory, Router, matchPromise } from '@americanexpress/one-app-router';
import { setModuleMap } from 'holocron';

import {
initializeClientStore, loadPrerenderScripts, moveHelmetScripts, loadServiceWorker,
} from './prerender';
import createRoutes from '../universal/routes';
import match from '../universal/utils/matchPromisified';


export default async function initClient() {
try {
Expand All @@ -39,7 +37,7 @@ export default async function initClient() {

await loadPrerenderScripts(store.getState());

const { redirectLocation, renderProps } = await match({ history, routes });
const { redirectLocation, renderProps } = await matchPromise({ history, routes });

if (redirectLocation) {
// FIXME: redirectLocation has pathname, query object, etc; need to format the URL better
Expand Down
5 changes: 2 additions & 3 deletions src/server/middleware/createRequestHtmlFragment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import React from 'react';
import { Provider } from 'react-redux';
import url, { Url } from 'url';
import { RouterContext } from '@americanexpress/one-app-router';
import { RouterContext, matchPromise } from '@americanexpress/one-app-router';
import { composeModules } from 'holocron';
import match from '../../universal/utils/matchPromisified';
import createCircuitBreaker from '../utils/createCircuitBreaker';

import { renderForString, renderForStaticMarkup } from '../utils/reactRendering';
Expand All @@ -38,7 +37,7 @@ export default function createRequestHtmlFragment({ createRoutes }) {
const { dispatch } = store;
const routes = createRoutes(store);

const { redirectLocation, renderProps } = await match({ routes, location: req.url });
const { redirectLocation, renderProps } = await matchPromise({ routes, location: req.url });
if (redirectLocation) {
// support redirecting outside our app (i.e. domain/origin)
// store more than pathname and search as a Url object as redirectLocation.state
Expand Down
28 changes: 0 additions & 28 deletions src/universal/utils/matchPromisified.js

This file was deleted.