|
1 |
| -import * as chai from 'chai' |
| 1 | +import { assert, expect } from 'chai' |
2 | 2 | import { describe, it } from 'mocha'
|
3 | 3 | import * as React from 'react'
|
4 | 4 | import { cleanup, fireEvent, render } from 'react-testing-library'
|
| 5 | +import { EMPTY, merge, of, Subject } from 'rxjs' |
| 6 | +import { switchMap, tap } from 'rxjs/operators' |
5 | 7 | import { TestScheduler } from 'rxjs/testing'
|
| 8 | +import * as sinon from 'sinon' |
6 | 9 |
|
7 |
| -import { EMPTY, merge, of, Subject } from 'rxjs' |
8 |
| -import { map, switchMap, switchMapTo, tap } from 'rxjs/operators' |
9 |
| -import { ServerURLForm } from './ServerURLForm' |
| 10 | +import { ServerURLForm, ServerURLFormProps } from './ServerURLForm' |
| 11 | + |
| 12 | +const noop = () => { |
| 13 | + /* noop */ |
| 14 | +} |
10 | 15 |
|
11 | 16 | describe('ServerURLForm', () => {
|
12 | 17 | after(cleanup)
|
13 | 18 |
|
14 |
| - it('the onChange prop handler gets fired', () => { |
15 |
| - const scheduler = new TestScheduler((a, b) => chai.assert.deepEqual(a, b)) |
| 19 | + it('fires the onChange prop handler', () => { |
| 20 | + const onChange = sinon.spy() |
| 21 | + const onSubmit = sinon.spy() |
| 22 | + |
| 23 | + const { container } = render( |
| 24 | + <ServerURLForm |
| 25 | + value={'https://sourcegraph.com'} |
| 26 | + status={'connected'} |
| 27 | + onChange={onChange} |
| 28 | + onSubmit={onSubmit} |
| 29 | + /> |
| 30 | + ) |
| 31 | + |
| 32 | + const urlInput = container.querySelector('input')! |
| 33 | + |
| 34 | + fireEvent.change(urlInput, { target: { value: 'https://different.com' } }) |
| 35 | + |
| 36 | + assert.isTrue(onChange.calledOnce) |
| 37 | + assert.isTrue(onChange.calledWith('https://different.com')) |
| 38 | + |
| 39 | + assert.isTrue(onSubmit.notCalled) |
| 40 | + }) |
| 41 | + |
| 42 | + it('updates the input value when the url changes', () => { |
| 43 | + const props: ServerURLFormProps = { |
| 44 | + value: 'https://sourcegraph.com', |
| 45 | + status: 'connected', |
| 46 | + onChange: noop, |
| 47 | + onSubmit: noop, |
| 48 | + } |
| 49 | + |
| 50 | + const { container, rerender } = render(<ServerURLForm {...props} />) |
| 51 | + |
| 52 | + const urlInput = container.querySelector('input')! |
| 53 | + |
| 54 | + rerender(<ServerURLForm {...props} value={'https://different.com'} />) |
| 55 | + |
| 56 | + const newValue = urlInput.value |
| 57 | + |
| 58 | + expect(newValue).to.equal('https://different.com') |
| 59 | + }) |
| 60 | + |
| 61 | + it('fires the onSubmit prop handler when the form is submitted', () => { |
| 62 | + const onSubmit = sinon.spy() |
| 63 | + |
| 64 | + const { container } = render( |
| 65 | + <ServerURLForm value={'https://sourcegraph.com'} status={'connected'} onChange={noop} onSubmit={onSubmit} /> |
| 66 | + ) |
| 67 | + |
| 68 | + const form = container.querySelector('form')! |
| 69 | + |
| 70 | + fireEvent.submit(form) |
| 71 | + |
| 72 | + assert.isTrue(onSubmit.calledOnce) |
| 73 | + }) |
| 74 | + |
| 75 | + it('fires the onSubmit prop handler after 5s on inactivity after a change', () => { |
| 76 | + const scheduler = new TestScheduler((a, b) => assert.deepEqual(a, b)) |
16 | 77 |
|
17 | 78 | scheduler.run(({ cold, expectObservable }) => {
|
| 79 | + const submits = new Subject<void>() |
| 80 | + const nextSubmit = () => submits.next() |
| 81 | + |
| 82 | + const { container } = render( |
| 83 | + <ServerURLForm |
| 84 | + value={'https://sourcegraph.com'} |
| 85 | + status={'connected'} |
| 86 | + onChange={noop} |
| 87 | + onSubmit={nextSubmit} |
| 88 | + /> |
| 89 | + ) |
| 90 | + |
| 91 | + const form = container.querySelector('input')! |
| 92 | + |
18 | 93 | const urls: { [key: string]: string } = {
|
19 | 94 | a: 'https://different.com',
|
20 | 95 | }
|
21 | 96 |
|
22 |
| - const urlChanges = cold('a', urls).pipe( |
23 |
| - map(url => { |
24 |
| - const changes = new Subject<string>() |
25 |
| - const nextChange = (value: string) => changes.next(value) |
26 |
| - |
27 |
| - const submits = new Subject<void>() |
28 |
| - const nextSubmit = () => submits.next() |
29 |
| - |
30 |
| - const { container } = render( |
31 |
| - <ServerURLForm |
32 |
| - value={'https://sourcegraph.com'} |
33 |
| - status={'connected'} |
34 |
| - onChange={nextChange} |
35 |
| - onSubmit={nextSubmit} |
36 |
| - /> |
37 |
| - ) |
38 |
| - |
39 |
| - const urlInput = container.querySelector('input')! |
40 |
| - |
41 |
| - return { changes, submits, url, urlInput } |
42 |
| - }), |
43 |
| - switchMap(({ changes, url, urlInput }) => { |
| 97 | + const submitObs = cold('a', urls).pipe( |
| 98 | + switchMap(url => { |
44 | 99 | const emit = of(undefined).pipe(
|
45 | 100 | tap(() => {
|
46 |
| - fireEvent.change(urlInput, { target: { value: url } }) |
| 101 | + fireEvent.change(form, { target: { value: url } }) |
47 | 102 | }),
|
48 | 103 | switchMap(() => EMPTY)
|
49 | 104 | )
|
50 | 105 |
|
51 |
| - return merge(changes, emit) |
| 106 | + return merge(submits, emit) |
52 | 107 | })
|
53 | 108 | )
|
54 | 109 |
|
55 |
| - const values: { [key: string]: string } = { |
56 |
| - a: 'https://different.com', |
57 |
| - } |
58 |
| - |
59 |
| - expectObservable(urlChanges).toBe('a', values) |
| 110 | + expectObservable(submitObs).toBe('5s a', { a: undefined }) |
60 | 111 | })
|
61 | 112 | })
|
62 | 113 |
|
63 |
| - it('captures the form submit event', () => { |
64 |
| - const scheduler = new TestScheduler((a, b) => chai.assert.deepEqual(a, b)) |
| 114 | + it("doesn't submit after 5 seconds if the form was submitted manually", () => { |
| 115 | + const scheduler = new TestScheduler((a, b) => assert.deepEqual(a, b)) |
65 | 116 |
|
66 | 117 | scheduler.run(({ cold, expectObservable }) => {
|
67 |
| - const urls: { [key: string]: string } = { |
68 |
| - a: 'https://different.com', |
| 118 | + const changes = new Subject<string>() |
| 119 | + const nextChange = () => changes.next() |
| 120 | + |
| 121 | + const submits = new Subject<void>() |
| 122 | + const nextSubmit = () => submits.next() |
| 123 | + |
| 124 | + const props: ServerURLFormProps = { |
| 125 | + value: 'https://sourcegraph.com', |
| 126 | + status: 'connected', |
| 127 | + onChange: nextChange, |
| 128 | + onSubmit: nextSubmit, |
69 | 129 | }
|
70 | 130 |
|
71 |
| - const urlChanges = cold('a', urls).pipe( |
72 |
| - map(url => { |
73 |
| - const changes = new Subject<string>() |
74 |
| - const nextChange = (value: string) => changes.next(value) |
75 |
| - |
76 |
| - const submits = new Subject<void>() |
77 |
| - const nextSubmit = () => submits.next() |
78 |
| - |
79 |
| - const { container } = render( |
80 |
| - <ServerURLForm |
81 |
| - value={'https://sourcegraph.com'} |
82 |
| - status={'connected'} |
83 |
| - onChange={nextChange} |
84 |
| - onSubmit={nextSubmit} |
85 |
| - /> |
86 |
| - ) |
| 131 | + const { container } = render(<ServerURLForm {...props} />) |
| 132 | + const form = container.querySelector('input')! |
87 | 133 |
|
88 |
| - const urlInput = container.querySelector('input')! |
| 134 | + changes.subscribe(url => { |
| 135 | + fireEvent.submit(form) |
| 136 | + }) |
89 | 137 |
|
90 |
| - return { changes, submits, url, urlInput } |
91 |
| - }), |
92 |
| - switchMap(({ changes, url, urlInput }) => { |
| 138 | + const urls: { [key: string]: string } = { |
| 139 | + a: 'https://different.com', |
| 140 | + } |
| 141 | + |
| 142 | + const submitObs = cold('a', urls).pipe( |
| 143 | + switchMap(url => { |
93 | 144 | const emit = of(undefined).pipe(
|
94 | 145 | tap(() => {
|
95 |
| - fireEvent.change(urlInput, { target: { value: url } }) |
| 146 | + fireEvent.change(form, { target: { value: url } }) |
96 | 147 | }),
|
97 | 148 | switchMap(() => EMPTY)
|
98 | 149 | )
|
99 | 150 |
|
100 |
| - return merge(changes, emit) |
| 151 | + return merge(submits, emit) |
101 | 152 | })
|
102 | 153 | )
|
103 | 154 |
|
104 |
| - const values: { [key: string]: string } = { |
105 |
| - a: 'https://different.com', |
106 |
| - } |
107 |
| - |
108 |
| - expectObservable(urlChanges).toBe('a', values) |
| 155 | + expectObservable(submitObs).toBe('a', { a: undefined }) |
109 | 156 | })
|
110 | 157 | })
|
111 |
| - |
112 |
| - // it('submits after 5 seconds of inactivity after a change', () => { |
113 |
| - // const scheduler = new TestScheduler((a, b) => chai.assert.deepEqual(a, b)) |
114 |
| - |
115 |
| - // scheduler.run(({ cold, expectObservable }) => { |
116 |
| - // const urls: { [key: string]: string } = { |
117 |
| - // a: 'https://different.com', |
118 |
| - // } |
119 |
| - |
120 |
| - // const inject = (nextChange: (value: string) => void, nextSubmit: () => void) => { |
121 |
| - // const { container, rerender } = render( |
122 |
| - // <ServerURLForm |
123 |
| - // value={'https://sourcegraph.com'} |
124 |
| - // status={'connected'} |
125 |
| - // onChange={nextChange} |
126 |
| - // onSubmit={nextSubmit} |
127 |
| - // /> |
128 |
| - // ) |
129 |
| - |
130 |
| - // const rr = (value: string) => |
131 |
| - // rerender( |
132 |
| - // <ServerURLForm value={value} status={'connected'} onChange={nextChange} onSubmit={nextSubmit} /> |
133 |
| - // ) |
134 |
| - |
135 |
| - // return { rerender: rr, input: container.querySelector('input')! } |
136 |
| - // } |
137 |
| - |
138 |
| - // const submits = cold('a', urls).pipe( |
139 |
| - // map(url => { |
140 |
| - // const changes = new Subject<string>() |
141 |
| - // const nextChange = (value: string) => changes.next(value) |
142 |
| - |
143 |
| - // const submits = new Subject<void>() |
144 |
| - // const nextSubmit = () => submits.next() |
145 |
| - |
146 |
| - // const { input, rerender } = inject(nextChange, nextSubmit) |
147 |
| - |
148 |
| - // return { changes, submits, url, input, rerender } |
149 |
| - // }), |
150 |
| - // switchMap(({ changes, submits, url, input, rerender }) => { |
151 |
| - // // const emit = of(undefined).pipe( |
152 |
| - // // tap(() => { |
153 |
| - // // fireEvent.change(input, { target: { value: url } }) |
154 |
| - // // }), |
155 |
| - // // switchMap(() => EMPTY) |
156 |
| - // // ) |
157 |
| - |
158 |
| - // const rerenders = changes.pipe( |
159 |
| - // tap(value => { |
160 |
| - // rerender(value) |
161 |
| - // }), |
162 |
| - // switchMapTo(EMPTY) |
163 |
| - // ) |
164 |
| - |
165 |
| - // return merge(submits, rerenders) |
166 |
| - // }) |
167 |
| - // ) |
168 |
| - |
169 |
| - // expectObservable(submits).toBe('5s a') |
170 |
| - // }) |
171 |
| - // }) |
172 | 158 | })
|
0 commit comments