forked from cypress-io/cypress-react-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard-spec.js
59 lines (54 loc) · 1.61 KB
/
card-spec.js
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
/// <reference types="cypress" />
import Card from './card.jsx'
import React from 'react'
import { mount } from 'cypress-react-unit-test'
// looking at the clock control from component's hook
// https://github.com/bahmutov/cypress-react-unit-test/issues/200
it('should select null after timing out (fast)', () => {
const onSelect = cy.stub()
// https://on.cypress.io/clock
cy.clock()
mount(<Card onSelect={onSelect} />)
cy.get('button').should('have.length', 4)
// component calls "onSelect" stub after 5 seconds of inactivity
cy.tick(100).then(() => {
// not yet
expect(onSelect).to.not.have.been.called
})
cy.tick(1000).then(() => {
// not yet
expect(onSelect).to.not.have.been.called
})
cy.tick(1000).then(() => {
// not yet
expect(onSelect).to.not.have.been.called
})
cy.tick(1000).then(() => {
// not yet
expect(onSelect).to.not.have.been.called
})
cy.tick(1000).then(() => {
// not yet
expect(onSelect).to.not.have.been.called
})
cy.log('5 seconds passed')
cy.tick(1000).then(() => {
// NOW
expect(onSelect).to.have.been.calledWith(null)
})
})
it('should select null after timing out (slow)', () => {
// without synthetic clock we must wait for the real delay
const onSelect = cy.stub().as('selected')
mount(<Card onSelect={onSelect} />)
cy.get('@selected', { timeout: 5100 }).should('have.been.calledWith', null)
})
it('should accept selections', () => {
const onSelect = cy.stub()
mount(<Card onSelect={onSelect} />)
cy.get("[data-testid='2']")
.click()
.then(() => {
expect(onSelect).to.have.been.calledWith(2)
})
})