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

Update Home Page content after cache hit (#2230) #2453

Merged
merged 12 commits into from
Jun 12, 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
3 changes: 2 additions & 1 deletion packages/peregrine/lib/store/actions/app/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const actionTypes = [
'SET_OFFLINE',
'TOGGLE_SEARCH',
'EXECUTE_SEARCH',
'MARK_ERROR_HANDLED'
'MARK_ERROR_HANDLED',
'SET_PAGE_LOADING'
];

export default createActions(...actionTypes, { prefix });
9 changes: 8 additions & 1 deletion packages/peregrine/lib/store/reducers/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const initialState = {
isOnline: navigator.onLine,
overlay: false,
searchOpen: false,
pending: {}
pending: {},
isPageLoading: false
};

const reducerMap = {
Expand Down Expand Up @@ -39,6 +40,12 @@ const reducerMap = {
isOnline: false,
hasBeenOffline: true
};
},
[actions.setPageLoading]: (state, { payload }) => {
davemacaulay marked this conversation as resolved.
Show resolved Hide resolved
return {
...state,
isPageLoading: !!payload
};
}
};

Expand Down
72 changes: 72 additions & 0 deletions packages/peregrine/lib/talons/Header/__tests__/useHeader.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React, { useEffect } from 'react';
import { useAppContext } from '@magento/peregrine/lib/context/app';
import { useHeader } from '../useHeader';
import { createTestInstance } from '../../../index';
import { act } from 'react-test-renderer';

jest.mock('@magento/peregrine/lib/context/app', () => {
const api = {};
const state = {};
return {
useAppContext: jest.fn(() => [state, api])
};
});

const log = jest.fn();
const Component = () => {
const talonProps = useHeader();

useEffect(() => {
log(talonProps);
}, [talonProps]);

return <div {...talonProps} id={'header'} />;
};

test('useHeader returns correct values from useAppContext', () => {
useAppContext.mockImplementation(() => {
return [
{
hasBeenOffline: false,
isOnline: true,
searchOpen: false,
isPageLoading: false
},
{
toggleSearch: jest.fn()
}
];
});

createTestInstance(<Component />);

expect(log).toHaveBeenCalledWith({
handleSearchTriggerClick: expect.any(Function),
hasBeenOffline: false,
isOnline: true,
isPageLoading: false,
searchOpen: false
});
});

test('handleSearchTriggerClick calls toggleSearch from useAppContext', () => {
const toggleSearchFn = jest.fn();

useAppContext.mockImplementation(() => {
return [
{},
{
toggleSearch: toggleSearchFn
}
];
});

const component = createTestInstance(<Component />);
const talonProps = component.root.findByProps({ id: 'header' }).props;

act(() => {
talonProps.handleSearchTriggerClick();
});

expect(toggleSearchFn).toHaveBeenCalled();
});
5 changes: 3 additions & 2 deletions packages/peregrine/lib/talons/Header/useHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useAppContext } from '@magento/peregrine/lib/context/app';

export const useHeader = () => {
const [
{ hasBeenOffline, isOnline, searchOpen },
{ hasBeenOffline, isOnline, isPageLoading, searchOpen },
{ toggleSearch }
] = useAppContext();

Expand All @@ -15,6 +15,7 @@ export const useHeader = () => {
handleSearchTriggerClick,
hasBeenOffline,
isOnline,
searchOpen
searchOpen,
isPageLoading
};
};
19 changes: 15 additions & 4 deletions packages/venia-ui/lib/RootComponents/CMS/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Meta, Title } from '../../components/Head';
import { mergeClasses } from '../../classify';

import defaultClasses from './cms.css';
import { useAppContext } from '@magento/peregrine/lib/context/app';

const CMSPage = props => {
const { id } = props;
Expand All @@ -17,8 +18,15 @@ const CMSPage = props => {
variables: {
id: Number(id),
onServer: false
}
},
fetchPolicy: 'cache-and-network'
});
const [
{ isPageLoading },
{
actions: { setPageLoading }
}
] = useAppContext();

if (error) {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -27,12 +35,15 @@ const CMSPage = props => {
return <div>Page Fetch Error</div>;
}

if (loading) {
if (!data) {
return fullPageLoadingIndicator;
}

if (!data) {
return null;
// Ensure we mark the page as loading while we check the network for updates
davemacaulay marked this conversation as resolved.
Show resolved Hide resolved
if (loading && !isPageLoading) {
setPageLoading(true);
} else if (!loading && isPageLoading) {
setPageLoading(false);
}

const { content_heading, title } = data.cmsPage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`verify Header can render in default state 1`] = `
<header
className="closed"
>
<div
className="toolbar"
>
<div
className="primaryActions"
>
<NavTrigger />
</div>
<OnlineIndicator
hasBeenOffline={false}
isOnline={true}
/>
<div
className="secondaryActions"
>
<SearchTrigger
active={false}
onClick={[MockFunction]}
/>
<CartTrigger />
</div>
</div>
</header>
`;
54 changes: 54 additions & 0 deletions packages/venia-ui/lib/components/Header/__tests__/header.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React from 'react';
import { createTestInstance } from '@magento/peregrine';
import Header from '../header';
import { useHeader } from '@magento/peregrine/lib/talons/Header/useHeader';

jest.mock('../../../classify');
jest.mock('../../Logo', () => 'Logo');
jest.mock('../cartTrigger', () => 'CartTrigger');
jest.mock('../navTrigger', () => 'NavTrigger');
jest.mock('../searchTrigger', () => 'SearchTrigger');
jest.mock('../onlineIndicator', () => 'OnlineIndicator');
jest.mock('../../PageLoadingIndicator', () => () => (
<div id={'pageLoadingIndicator'} />
));

jest.mock('@magento/venia-drivers', () => ({
resourceUrl: jest.fn(url => url),
Link: jest.fn(() => null),
Route: jest.fn(() => null)
}));

jest.mock('@magento/peregrine/lib/talons/Header/useHeader', () => {
const state = {
handleSearchTriggerClick: jest.fn(),
hasBeenOffline: false,
isOnline: true,
searchOpen: false,
isPageLoading: false
};
return {
useHeader: jest.fn(() => state)
};
});

test('verify Header can render in default state', () => {
const component = createTestInstance(<Header />);

expect(component.toJSON()).toMatchSnapshot();
});

test('verify PageLoadingIndicator is displayed when page is loading', () => {
useHeader.mockImplementation(() => {
return {
handleSearchTriggerClick: jest.fn(),
hasBeenOffline: false,
isOnline: true,
searchOpen: false,
isPageLoading: true
};
});

const component = createTestInstance(<Header />);
component.root.findByProps({ id: 'pageLoadingIndicator' });
});
3 changes: 3 additions & 0 deletions packages/venia-ui/lib/components/Header/header.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
.primaryActions {
grid-area: primary;
justify-self: start;
display: grid;
grid-auto-flow: column;
align-items: center;
}

.secondaryActions {
Expand Down
8 changes: 7 additions & 1 deletion packages/venia-ui/lib/components/Header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useHeader } from '@magento/peregrine/lib/talons/Header/useHeader';

import { mergeClasses } from '../../classify';
import defaultClasses from './header.css';
import PageLoadingIndicator from '../PageLoadingIndicator';

const SearchBar = React.lazy(() => import('../SearchBar'));

Expand All @@ -20,7 +21,8 @@ const Header = props => {
handleSearchTriggerClick,
hasBeenOffline,
isOnline,
searchOpen
searchOpen,
isPageLoading
} = useHeader();

const classes = mergeClasses(defaultClasses, props.classes);
Expand All @@ -39,12 +41,16 @@ const Header = props => {
</Route>
</Suspense>
) : null;
const pageLoadingIndicator = isPageLoading ? (
<PageLoadingIndicator />
) : null;

return (
<header className={rootClass}>
<div className={classes.toolbar}>
<div className={classes.primaryActions}>
<NavTrigger />
{pageLoadingIndicator}
</div>
<OnlineIndicator
hasBeenOffline={hasBeenOffline}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './indicator';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.root {
justify-self: start;
animation: spin 1920ms linear infinite;
}

.indicator {
--stroke: rgb(var(--venia-text-hint));
}

@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
22 changes: 22 additions & 0 deletions packages/venia-ui/lib/components/PageLoadingIndicator/indicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

import defaultClasses from './indicator.css';
import { mergeClasses } from '../../classify';
import { RotateCw as LoaderIcon } from 'react-feather';
import Icon from '../Icon';

const PageLoadingIndicator = props => {
const classes = mergeClasses(defaultClasses, props.classes);

return (
<div className={classes.root}>
<Icon
src={LoaderIcon}
size={24}
classes={{ root: classes.indicator }}
/>
</div>
);
};

export default PageLoadingIndicator;