-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-random-user-spec.ts
97 lines (88 loc) · 2.14 KB
/
http-random-user-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// <reference types="Cypress" />
import { Stream } from 'xstream';
import { div, button, h1, h4, a, DOMSource } from '@cycle/dom';
import { makeHTTPDriver, Response, HTTPSource } from '@cycle/http';
import { mount } from '../..'
type UserData = {
id: number,
name: string,
username: string,
email: string,
address: {
street: string,
suite: string,
city: string,
zipcode: string,
geo: {
lat: string,
lng: string,
},
},
phone: string,
website: string,
company: {
name: string,
catchPhrase: string,
bs: string,
},
};
function main(sources: { DOM: DOMSource, HTTP: HTTPSource }) {
const getRandomUser$ = sources.DOM.select('.get-random').events('click')
.map(() => {
const randomNum = Math.round(Math.random() * 9) + 1;
return {
url: 'https://jsonplaceholder.typicode.com/users/' + String(randomNum),
category: 'users',
method: 'GET',
};
});
const user$ = sources.HTTP.select('users')
.flatten()
.map(res => res.body as UserData)
.startWith(null);
const vdom$ = user$.map(user =>
div('.users', [
button('.get-random', 'Get random user'),
user === null ? null : div('.user-details', [
h1('.user-name', user.name),
h4('.user-email', user.email),
a('.user-website', { attrs: { href: user.website } }, user.website),
]),
]),
);
return {
DOM: vdom$,
HTTP: getRandomUser$,
};
}
/* eslint-env mocha */
describe('Http random user', () => {
beforeEach(() => {
mount(main, {
HTTP: makeHTTPDriver()
})
})
it('loads random user', () => {
cy.server()
cy.route('/users/*').as('xhr')
cy.get('.get-random').click()
cy.get('@xhr')
cy.get('.user-details').should('be.visible')
})
it('stubs random user XHR', () => {
const user = {
id: 101,
name: 'Test User',
email: '[email protected]',
website: 'https://cypress.io'
}
cy.server()
cy.route({
url: '/users/*',
response: user
})
cy.get('.get-random').click()
cy.get('.user-details').should('be.visible')
.contains('[email protected]')
})
})