Skip to content

Commit

Permalink
add visual test lib, loki and bump up versions
Browse files Browse the repository at this point in the history
  • Loading branch information
narr committed Aug 22, 2020
1 parent 7e775f6 commit 95ce4c1
Show file tree
Hide file tree
Showing 19 changed files with 1,158 additions and 286 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
'react-hooks/rules-of-hooks': 'error',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{ ignoreRestSiblings: true },
Expand Down
2 changes: 2 additions & 0 deletions .loki/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
current
difference
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { RouterContext } from 'next/dist/next-server/lib/router-context';
import 'loki/configure-react';
// NOTE: can add a global CSS

export const parameters = {
Expand Down
1 change: 1 addition & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@loki/create-async-callback';
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ yarn test

It will open Cypress for Unit tests and E2E tests.

Third, run visual regression tests:

```bash
# https://loki.js.org/command-line-arguments.html#loki-test

# to test all stories
yarn storybook
yarn test:visual

# to use regular expression for stories to test
# Below will only test stories whose name has the string, 'card'
yarn test:visual:filter card
```

## Build

Run the development server with build files:
Expand Down
2 changes: 1 addition & 1 deletion cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Cypress.Commands.add(
reqAfterCallback,
selectorToCheckUiRenderDone,
}) => {
cy.setCookie('mock_server', 'true');
cy.setCookie('no_mock_api', 'false');
cy.visit(url, {
onBeforeLoad: win => {
win.eval(window.parent.xhookResponse);
Expand Down
11 changes: 11 additions & 0 deletions loki.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
configurations: {
'chrome.laptop': {
target: 'chrome.app',
width: 1366,
height: 768,
deviceScaleFactor: 1,
mobile: false,
},
},
};
22 changes: 15 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"test": "cross-env COVERAGE=true cypress open",
"test:run": "cross-env COVERAGE=true cypress run --browser chrome --headless --config baseUrl=http://localhost:6007",
"test:ci": "start-server-and-test storybook:ci http://localhost:6007 test:run",
"test:visual": "yarn loki test",
"test:visual:filter": "loki test --storiesFilter",
"test:visual:update": "loki update",
"test:visual:approve": "loki approve",
"test:visual:ci:base": "loki test --reactPort 6007",
"test:visual:ci": "start-server-and-test storybook:ci http://localhost:6007 test:visual:ci:base",
"ci": "yarn lint && yarn test:ci",
"build": "next build && next export",
"build:analyze": "cross-env ANALYZE=true yarn build",
Expand All @@ -23,7 +29,7 @@
"dependencies": {
"@material-ui/core": "^4.11.0",
"@material-ui/icons": "^4.9.1",
"axios": "^0.19.2",
"axios": "^0.20.0",
"d3": "^5.16.0",
"next": "^9.5.2",
"react": "^16.13.1",
Expand All @@ -32,11 +38,12 @@
"devDependencies": {
"@cypress/code-coverage": "^3.8.1",
"@cypress/webpack-preprocessor": "^5.4.4",
"@loki/create-async-callback": "^0.24.0",
"@next/bundle-analyzer": "^9.5.2",
"@storybook/addon-actions": "^6.0.13",
"@storybook/addon-essentials": "^6.0.13",
"@storybook/addon-links": "^6.0.13",
"@storybook/react": "^6.0.13",
"@storybook/addon-actions": "^6.0.16",
"@storybook/addon-essentials": "^6.0.16",
"@storybook/addon-links": "^6.0.16",
"@storybook/react": "^6.0.16",
"@types/d3": "^5.7.2",
"@types/react": "^16.9.46",
"@typescript-eslint/eslint-plugin": "^3.9.1",
Expand All @@ -45,7 +52,7 @@
"cookie-parser": "^1.4.5",
"cors": "^2.8.5",
"cross-env": "^7.0.2",
"cypress": "^4.12.1",
"cypress": "^5.0.0",
"eslint": "^7.7.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-cypress": "^2.11.1",
Expand All @@ -54,11 +61,12 @@
"eslint-plugin-react-hooks": "^4.1.0",
"express": "^4.17.1",
"http-proxy-middleware": "^1.0.5",
"loki": "^0.24.0",
"lowdb": "^1.0.0",
"morgan": "^1.10.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"start-server-and-test": "^1.11.3",
"typescript": "^3.9.7"
"typescript": "^4.0.2"
}
}
14 changes: 7 additions & 7 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ module.exports = app => {
app.use(express.urlencoded({ extended: true }));

app.use('/api/posts', (req, res, next) => {
if (req.cookies.mock_server === 'true') {
timeouts.read();
const timeout = timeouts.get('getPosts') || 0;
setTimeout(() => {
postCtrl.getPosts(req, res);
}, timeout);
if (req.cookies.no_mock_api === 'true') {
next();
return;
}
next();
timeouts.read();
const timeout = timeouts.get('getPosts') || 0;
setTimeout(() => {
postCtrl.getPosts(req, res);
}, timeout);
});

setProxy(app);
Expand Down
9 changes: 8 additions & 1 deletion src/components/Posts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ type Post = {
body: string;
};

export interface PostsProps {
onPostsLoad?: VoidFunction;
}

const API_URL_POST =
process.env.NODE_ENV === 'production'
? `https://jsonplaceholder.typicode.com`
: `/api`;

export const Posts = () => {
export const Posts = (props: PostsProps) => {
const { onPostsLoad = () => {} } = props;

const [posts, setPosts] = useState<Post[]>([]);
const [isLoading, setIsLoading] = useState(true);

Expand All @@ -29,6 +35,7 @@ export const Posts = () => {
.then(response => {
setPosts(response.data);
setIsLoading(false);
onPostsLoad();
})
.catch(error => {
if (axios.isCancel(error)) {
Expand Down
9 changes: 9 additions & 0 deletions src/components/StorybookExample/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ export const Secondary = Template.bind({});
Secondary.args = {
label: 'Button',
};
Secondary.parameters = {
loki: { skip: true },
};

export const Large = Template.bind({});
Large.args = {
size: 'large',
label: 'Button',
};
Large.parameters = {
loki: { skip: true },
};

export const Small = Template.bind({});
Small.args = {
size: 'small',
label: 'Button',
};
Small.parameters = {
loki: { skip: true },
};
11 changes: 7 additions & 4 deletions src/pages/test/posts.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import React from 'react';
import { GlobalCss } from '../../components/GlobalCss';
import Head from 'next/head';
import { Posts } from '../../components/Posts';
import { Posts, PostsProps } from '../../components/Posts';
import { useRouter } from 'next/router';
import FaceIcon from '@material-ui/icons/Face';

const PostsPage = () => {
export type PostsPageProps = PostsProps;

const PostsPage = (props: PostsPageProps) => {
const { onPostsLoad } = props;
const router = useRouter();
return (
<>
Expand All @@ -23,10 +26,10 @@ const PostsPage = () => {
</h1>
<p className="description">
Get started by editing{' '}
<code>{`src/pages${router.pathname}.tsx`}</code>
<code>{`src/pages${router.pathname || '/test/posts'}.tsx`}</code>
</p>
<FaceIcon />
<Posts />
<Posts onPostsLoad={onPostsLoad} />
</main>

<style jsx>{`
Expand Down
17 changes: 15 additions & 2 deletions src/stories/test/posts.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';
import { Story, Meta } from '@storybook/react/types-6-0';
import createAsyncCallback from '@loki/create-async-callback';

import PostsPage from '../../pages/test/posts';
import PostsPage, { PostsPageProps } from '../../pages/test/posts';

export default {
title: 'Test/PostsPage',
Expand All @@ -12,7 +13,19 @@ export default {
},
} as Meta;

const Template: Story = args => <PostsPage {...args} />;
const Template: Story<PostsPageProps> = args => {
// NOTE: createAsyncCallback doesn't work if it is passed by args like example below
// it hangs when running loki test
// Base.args = {
// onPostsLoad: createAsyncCallback(),
// };
return (
<PostsPage
{...args}
onPostsLoad={args.onPostsLoad ? args.onPostsLoad : createAsyncCallback()}
/>
);
};

export const Base = Template.bind({});
Base.args = {};
Loading

1 comment on commit 95ce4c1

@vercel
Copy link

@vercel vercel bot commented on 95ce4c1 Aug 22, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.