Skip to content

Commit 012bc7c

Browse files
committed
chore: update e2e test code
1 parent b931a6d commit 012bc7c

File tree

9 files changed

+631
-29
lines changed

9 files changed

+631
-29
lines changed

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const basePath = path.resolve(__dirname, '../', 'src');
33

44
module.exports = {
5-
stories: ['../@(stories|example)/*.stories.@(ts|tsx)'],
5+
stories: ['../stories/**/*.stories.@(jsx|tsx)'],
66
addons: [
77
'@storybook/addon-actions',
88
'@storybook/addon-links',

example/Demo.stories.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ export const Demo = () => {
99

1010
return (
1111
<>
12-
<button onClick={() => scrollTo('#y-item-20')}>
12+
<button onClick={() => scrollTo('#y-item-20')} id={'demo-stories-btn-1'}>
1313
scrollTo('#y-item-20')
1414
</button>
15-
<button onClick={() => scrollTo(400)}>scrollTo(400)</button>
15+
<button onClick={() => scrollTo(400)} id={'demo-stories-btn-2'}>
16+
scrollTo(400)
17+
</button>
1618
<br />
1719
<div
20+
id={'demo-stories'}
1821
ref={ref}
1922
style={{
2023
overflowY: 'scroll',

jest-puppeteer.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
server: {
3+
command: 'npm run storybook',
4+
port: 6006,
5+
launchTimeout: 100000,
6+
debug: true,
7+
usedPortAction: 'ignore',
8+
},
9+
launch: {
10+
// headless: false,
11+
},
12+
};

jest.e2e.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
module.exports = {
22
preset: 'jest-puppeteer-preset',
33
roots: ['<rootDir>/test/e2e/'],
4+
globals: {
5+
'ts-jest': {
6+
diagnostics: false,
7+
},
8+
},
49
};

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,25 @@
6060
"@storybook/storybook-deployer": "^2.8.6",
6161
"@types/react": "^16.9.46",
6262
"@types/react-dom": "^16.9.8",
63+
"add": "^2.0.6",
6364
"babel-loader": "^8.1.0",
65+
"enzyme": "^3.11.0",
66+
"enzyme-adapter-react-16": "^1.15.5",
6467
"husky": "^4.2.5",
6568
"jest-enzyme": "^7.1.2",
6669
"jest-puppeteer-preset": "^4.4.0",
70+
"puppeteer": "^5.3.1",
6771
"react": "^16.13.1",
6872
"react-docgen-typescript-loader": "^3.7.2",
6973
"react-dom": "^16.13.1",
7074
"react-is": "^16.13.1",
7175
"semantic-release": "^17.1.1",
76+
"ts-jest": "^26.4.0",
7277
"ts-loader": "^8.0.2",
7378
"tsdx": "^0.13.2",
7479
"tslib": "^2.0.1",
75-
"typescript": "^4.0.2"
80+
"typescript": "^4.0.2",
81+
"yarn": "^1.22.10"
7682
},
7783
"publishConfig": {
7884
"access": "public"

src/useSmoothScroll.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type UseSmoothScrollType = {
2121
};
2222

2323
// get the relative distance from destination
24-
const getRelativeDistance = (
24+
export const getRelativeDistance = (
2525
target: number | string | undefined,
2626
parent: HTMLElement,
2727
attrMap: AttrMapType

test/e2e/useSmoothScroll.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var fs = require('fs');
2+
describe('useSmoothScroll', () => {
3+
// jest.setTimeout(30000);
4+
const CommonUsage = async () => {
5+
beforeEach(async () => {
6+
console.log('------------beforeEach');
7+
const headlessUserAgent = await page.evaluate(() => navigator.userAgent);
8+
const chromeUserAgent = headlessUserAgent.replace(
9+
'HeadlessChrome',
10+
'Chrome'
11+
);
12+
await page.setUserAgent(chromeUserAgent);
13+
await page.setExtraHTTPHeaders({
14+
'accept-language': 'en-US,en;q=0.8',
15+
});
16+
// await page.setViewport({
17+
// width: 1024,
18+
// height: 768,
19+
// });
20+
// hack:load storybook's iframe directly to avoid bug
21+
await page.goto(
22+
'http://localhost:6006/iframe.html?id=main-usesmoothscroll--docs&viewMode=docs#stories'
23+
);
24+
await page.waitForSelector('#root');
25+
26+
// test code below
27+
// await page.waitFor(3000);
28+
// const test = await page.content();
29+
// fs.writeFileSync('writeMe.html', test);
30+
});
31+
32+
it('should scrollTo target node when button clicked', async () => {
33+
await expect(page).toMatch('y-item-0');
34+
const demoWrapElm = await page.$(`#demo-stories`);
35+
const getItem = async num => {
36+
const res = await page.$(`#y-item-${num}`);
37+
return res;
38+
};
39+
expect(await (await getItem(20)).isIntersectingViewport()).toBe(false);
40+
41+
// await expect(page).toClick('button', { text: `scrollTo('#y-item-20')` });
42+
await (await page.$('#demo-stories-btn-1')).click();
43+
await page.waitFor(1000);
44+
45+
// expect(await (await getItem(28)).isIntersectingViewport()).toBe(true);
46+
// await expect(page).toMatchElement('#y-item-20');
47+
expect(
48+
await (await demoWrapElm.getProperty('scrollTop')).jsonValue()
49+
).toBe(430);
50+
});
51+
};
52+
CommonUsage();
53+
});

test/specs/utils.test.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { getAttrMap } from '../../src/utils';
2+
import { getCurIndex } from '../../src/useScrollWatch';
3+
describe('test src/utils', () => {
4+
describe('getAttrMap', () => {
5+
it('should return exact map when pass x', () => {
6+
expect(getAttrMap('x')).toMatchObject({
7+
scrollLeftTop: 'scrollLeft',
8+
scrollWidthHeight: 'scrollWidth',
9+
clientWidthHeight: 'clientWidth',
10+
offsetLeftTop: 'offsetLeft',
11+
offsetWidthHeight: 'offsetWidth',
12+
leftTop: 'left',
13+
});
14+
});
15+
it('should return exact map when pass y', () => {
16+
expect(getAttrMap('y')).toMatchObject({
17+
scrollLeftTop: 'scrollTop',
18+
scrollWidthHeight: 'scrollHeight',
19+
clientWidthHeight: 'clientHeight',
20+
offsetLeftTop: 'offsetTop',
21+
offsetWidthHeight: 'offsetHeight',
22+
leftTop: 'top',
23+
});
24+
});
25+
});
26+
27+
describe('getCurIndex', () => {
28+
expect(getCurIndex(0, [])).toBe(-1);
29+
expect(getCurIndex(100, [101, 200])).toBe(-1);
30+
expect(getCurIndex(100, [100, 200])).toBe(0);
31+
expect(getCurIndex(102, [101, 200])).toBe(0);
32+
expect(getCurIndex(199, [101, 200])).toBe(0);
33+
expect(getCurIndex(200, [101, 200])).toBe(1);
34+
});
35+
});

0 commit comments

Comments
 (0)