1
1
import * as React from 'react' ;
2
2
3
- import { shallow } from 'enzyme' ;
3
+ import { render } from '@testing-library/react' ;
4
+ import userEvent from '@testing-library/user-event' ;
4
5
import { mocked } from 'jest-mock' ;
5
6
6
7
import {
7
- ElectronReleaseChannel ,
8
8
InstallState ,
9
9
RunnableVersion ,
10
10
VersionSource ,
11
- } from '../../../ src/interfaces' ;
11
+ } from '../../src/interfaces' ;
12
12
import {
13
13
VersionSelect ,
14
14
filterItems ,
15
15
getItemIcon ,
16
16
getItemLabel ,
17
17
renderItem ,
18
- } from '../../../src/renderer/components/version-select' ;
19
- import { AppState } from '../../../src/renderer/state' ;
20
- import { disableDownload } from '../../../src/renderer/utils/disable-download' ;
21
- import { StateMock , VersionsMock } from '../../mocks/mocks' ;
18
+ } from '../../src/renderer/components/version-select' ;
19
+ import { disableDownload } from '../../src/renderer/utils/disable-download' ;
20
+ import { mockVersion1 , prepareAppState } from '../test-utils/versions' ;
22
21
23
22
const { downloading, installed, missing, installing } = InstallState ;
24
- const { remote , local } = VersionSource ;
23
+ const { local } = VersionSource ;
25
24
26
- jest . mock ( '../../../ src/renderer/utils/disable-download.ts' ) ;
25
+ jest . mock ( '../../src/renderer/utils/disable-download.ts' ) ;
27
26
28
27
describe ( 'VersionSelect component' , ( ) => {
29
- let store : AppState ;
30
-
31
- const mockVersion1 = {
32
- source : remote ,
33
- state : missing ,
34
- version : '1.0.0' ,
35
- } ;
36
-
37
- const mockVersion2 = {
38
- source : remote ,
39
- state : missing ,
40
- version : '3.0.0-unsupported' ,
41
- } ;
42
-
43
- beforeEach ( ( ) => {
44
- ( { state : store } = window . app ) ;
45
-
46
- const { mockVersions } = new VersionsMock ( ) ;
47
- ( store as unknown as StateMock ) . initVersions ( '2.0.2' , {
48
- ...mockVersions ,
49
- '1.0.0' : { ...mockVersion1 } ,
50
- '3.0.0-unsupported' : { ...mockVersion2 } ,
51
- } ) ;
52
- store . channelsToShow = [
53
- ElectronReleaseChannel . stable ,
54
- ElectronReleaseChannel . beta ,
55
- ] ;
56
- } ) ;
57
-
58
- const onVersionSelect = ( ) => ( { } ) ;
28
+ function renderVersionSelect ( ) {
29
+ const appState = prepareAppState ( ) ;
59
30
60
- it ( 'renders' , ( ) => {
61
- const wrapper = shallow (
31
+ return render (
62
32
< VersionSelect
63
- appState = { store }
33
+ appState = { appState }
64
34
currentVersion = { mockVersion1 }
65
- onVersionSelect = { onVersionSelect }
35
+ onVersionSelect = { jest . fn ( ) }
66
36
/> ,
67
37
) ;
68
- expect ( wrapper ) . toMatchSnapshot ( ) ;
69
- } ) ;
38
+ }
70
39
71
40
describe ( 'renderItem()' , ( ) => {
72
- it ( 'renders an item' , ( ) => {
73
- const item = renderItem ( mockVersion1 , {
74
- handleClick : ( ) => ( { } ) ,
75
- index : 0 ,
76
- modifiers : { active : true , disabled : false , matchesPredicate : true } ,
77
- query : '' ,
78
- } ) ;
79
-
80
- expect ( item ) . toMatchSnapshot ( ) ;
81
- } ) ;
82
-
83
41
it ( 'returns null if it does not match predicate' , ( ) => {
84
42
const item = renderItem ( mockVersion1 , {
85
43
handleClick : ( ) => ( { } ) ,
@@ -103,9 +61,9 @@ describe('VersionSelect component', () => {
103
61
query : '' ,
104
62
} ) ! ;
105
63
106
- const ItemWrapper = shallow ( item ) ;
64
+ const { getAllByTestId } = render ( item ) ;
107
65
108
- expect ( ItemWrapper . find ( '. disabled-menu-tooltip ') ) . toHaveLength ( 1 ) ;
66
+ expect ( getAllByTestId ( ' disabled-menu-item ') ) . toHaveLength ( 1 ) ;
109
67
} ) ;
110
68
111
69
it ( 'does not disable enabled download buttons when return value is false' , ( ) => {
@@ -118,9 +76,9 @@ describe('VersionSelect component', () => {
118
76
query : '' ,
119
77
} ) ! ;
120
78
121
- const ItemWrapper = shallow ( item ) ;
79
+ const { queryAllByTestId } = render ( item ) ;
122
80
123
- expect ( ItemWrapper . exists ( '. disabled-menu-tooltip ') ) . toBe ( false ) ;
81
+ expect ( queryAllByTestId ( ' disabled-menu-item ') ) . toHaveLength ( 0 ) ;
124
82
} ) ;
125
83
} ) ;
126
84
@@ -233,4 +191,23 @@ describe('VersionSelect component', () => {
233
191
expect ( filterItems ( 'nightly' , versions ) ) . toEqual ( expected ) ;
234
192
} ) ;
235
193
} ) ;
194
+
195
+ describe ( 'renderVersionContextMenu()' , ( ) => {
196
+ it ( 'copies the current version number to the clipboard' , async ( ) => {
197
+ const spy = jest
198
+ . spyOn ( navigator . clipboard , 'writeText' )
199
+ . mockImplementationOnce ( jest . fn ( ) ) ;
200
+
201
+ const { getByRole, getByText } = renderVersionSelect ( ) ;
202
+
203
+ await userEvent . pointer ( {
204
+ keys : '[MouseRight]' ,
205
+ target : getByRole ( 'button' ) ,
206
+ } ) ;
207
+
208
+ await userEvent . click ( getByText ( / c o p y v e r s i o n n u m b e r / i) ) ;
209
+
210
+ expect ( spy ) . toHaveBeenCalledWith ( mockVersion1 . version ) ;
211
+ } ) ;
212
+ } ) ;
236
213
} ) ;
0 commit comments