Skip to content

Commit 303daef

Browse files
committed
e2e test cypress-graphql-mock-network
1 parent 204075e commit 303daef

7 files changed

Lines changed: 619 additions & 386 deletions

File tree

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ jobs:
2424
uses: actions/setup-node@v1
2525
with:
2626
node-version: ${{ matrix.node-version }}
27-
- run: yarn
27+
- run: yarn --ignore-engines
2828
- run: yarn build
2929
- run: yarn test:all --updateSnapshot

e2e/App/index.e2e-test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { expect } from 'chai'
2+
import { initMockServer } from '../mock'
23

34
describe('App', () => {
5+
beforeEach(() => {
6+
initMockServer({
7+
authed: false,
8+
})
9+
})
10+
411
before(() => {
512
cy.visit('/')
613
})
714

15+
after(() => {
16+
cy.mockNetworkReset()
17+
})
18+
819
describe('Load App', () => {
920
it('Check content', () => {
1021
cy.contains('#__next > h2', 'My awesome component')
@@ -14,6 +25,51 @@ describe('App', () => {
1425
})
1526
})
1627
})
28+
29+
describe('Test mock API', () => {
30+
it('Mock API', () => {
31+
cy.mockNetworkAdd({
32+
Query: () => ({
33+
me: () => {
34+
return {
35+
id: 'test-user-1',
36+
username: 'test-user',
37+
fullname: 'Test User',
38+
}
39+
},
40+
}),
41+
})
42+
43+
return fetch('/api', {
44+
method: 'POST',
45+
body: JSON.stringify(
46+
{
47+
query: `
48+
query me {
49+
me {
50+
id
51+
}
52+
}
53+
`,
54+
variables: {},
55+
},
56+
undefined,
57+
2
58+
),
59+
headers: {
60+
'Content-Type': 'application/json',
61+
},
62+
})
63+
.then(async (r) => {
64+
const response = await r.json()
65+
return response
66+
})
67+
.then((result) => {
68+
expect(result.data).not.undefined
69+
expect(result.data?.me).not.null
70+
})
71+
})
72+
})
1773
})
1874

1975
export default true

e2e/mock/index.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import 'cypress-graphql-mock-network'
2+
import { MockNetworkOptions } from 'cypress-graphql-mock-network'
3+
4+
type initMockServerProps = Omit<MockNetworkOptions, 'schema'> & {
5+
/**
6+
* If false, me query returns null
7+
*/
8+
authed?: boolean
9+
}
10+
11+
export const initMockServer = (props?: initMockServerProps) => {
12+
const mocks: initMockServerProps['mocks'] = {
13+
...((props && props.mocks) || {}),
14+
}
15+
16+
if (props?.authed === false) {
17+
mocks.Query = (source, args, ctx, info) => {
18+
const fields = {
19+
me: () => {
20+
return null
21+
},
22+
usersConnection: () => {
23+
return {
24+
aggregate: {
25+
count: 0,
26+
},
27+
edges: [],
28+
}
29+
},
30+
}
31+
32+
if (props && props.mocks && props.mocks.Query) {
33+
Object.assign(fields, props.mocks.Query(source, args, ctx, info))
34+
}
35+
36+
return fields
37+
}
38+
}
39+
40+
const schema = `
41+
type User {
42+
id: String!
43+
}
44+
45+
type Query {
46+
me: User!
47+
}
48+
`
49+
50+
return cy.mockNetwork({
51+
schema,
52+
mocks: {
53+
DateTime: () => {
54+
return new Date()
55+
},
56+
Json: () => {
57+
return {}
58+
},
59+
...mocks,
60+
},
61+
})
62+
}

e2e/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
// https://github.com/scottohara/loot/issues/185
5+
"target": "ES2019"
6+
}
7+
}

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"@testing-library/react-hooks": "^3.4.2",
5050
"@testing-library/user-event": "^12.1.10",
5151
"@types/chai": "^4.2.14",
52-
"@types/cypress": "^1.1.3",
5352
"@types/express": "^4.17.9",
5453
"@types/http-proxy-middleware": "^0.19.3",
5554
"@types/jest": "^26.0.15",
@@ -62,7 +61,8 @@
6261
"chai": "^4.2.0",
6362
"cross-env": "^7.0.2",
6463
"css-loader": "^5.0.1",
65-
"cypress": "^6.2.0",
64+
"cypress": "^9.5.3",
65+
"cypress-graphql-mock-network": "^0.2.2",
6666
"dotenv": "^8.2.0",
6767
"eslint": "^7.0.0",
6868
"eslint-config-prettier": "^6.10.1",
@@ -72,6 +72,7 @@
7272
"eslint-plugin-react-hooks": "^4.2.0",
7373
"http-proxy-middleware": "^1.0.6",
7474
"husky": "^4.3.0",
75+
"immer": "^9.0.12",
7576
"jest": "^26.6.3",
7677
"jest-styled-components": "^7.0.3",
7778
"jest-watch-typeahead": "^0.5.0",
@@ -93,7 +94,9 @@
9394
"typescript": "^4.5.2",
9495
"typescript-styled-plugin": "^0.15.0"
9596
},
96-
"resolutions": {},
97+
"resolutions": {
98+
"immer": "^9.0.12"
99+
},
97100
"browserslist": {
98101
"production": [
99102
">0.2%",

0 commit comments

Comments
 (0)