@@ -15,52 +15,63 @@ limitations under the License.
15
15
*/
16
16
17
17
import React from 'react' ;
18
- import TestRenderer from 'react-test-renderer' ;
18
+ import { mount } from 'enzyme' ;
19
+ import { act } from 'react-dom/test-utils' ;
19
20
20
21
import sdk from '../../../skinned-sdk' ;
21
22
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg' ;
22
23
import { stubClient } from '../../../test-utils' ;
24
+ import { findById , flushPromises } from '../../../utils/test-utils' ;
23
25
24
26
const AccessSecretStorageDialog = sdk . getComponent ( "dialogs.security.AccessSecretStorageDialog" ) ;
25
27
26
28
describe ( "AccessSecretStorageDialog" , function ( ) {
27
- it ( "Closes the dialog if _onRecoveryKeyNext is called with a valid key" , ( done ) => {
28
- const testInstance = TestRenderer . create (
29
+ it ( "Closes the dialog if _onRecoveryKeyNext is called with a valid key" , async ( ) => {
30
+ const onFinished = jest . fn ( ) ;
31
+ const checkPrivateKey = jest . fn ( ) . mockResolvedValue ( true ) ;
32
+ const wrapper = mount (
29
33
< AccessSecretStorageDialog
30
- checkPrivateKey = { ( p ) => p && p . recoveryKey && p . recoveryKey == "a" }
31
- onFinished = { ( v ) => {
32
- if ( v ) { done ( ) ; }
33
- } }
34
+ checkPrivateKey = { checkPrivateKey }
35
+ onFinished = { onFinished }
34
36
/> ,
35
37
) ;
36
- testInstance . getInstance ( ) . setState ( {
38
+ wrapper . setState ( {
37
39
recoveryKeyValid : true ,
38
40
recoveryKey : "a" ,
39
41
} ) ;
40
42
const e = { preventDefault : ( ) => { } } ;
41
- testInstance . getInstance ( ) . onRecoveryKeyNext ( e ) ;
43
+
44
+ wrapper . find ( 'form' ) . simulate ( 'submit' , e ) ;
45
+
46
+ await flushPromises ( ) ;
47
+
48
+ expect ( checkPrivateKey ) . toHaveBeenCalledWith ( { recoveryKey : "a" } ) ;
49
+ expect ( onFinished ) . toHaveBeenCalledWith ( { recoveryKey : "a" } ) ;
42
50
} ) ;
43
51
44
52
it ( "Considers a valid key to be valid" , async function ( ) {
45
- const testInstance = TestRenderer . create (
53
+ const wrapper = mount (
46
54
< AccessSecretStorageDialog
47
55
checkPrivateKey = { ( ) => true }
48
56
/> ,
49
57
) ;
50
- const v = "asdf" ;
51
- const e = { target : { value : v } } ;
52
58
stubClient ( ) ;
53
59
MatrixClientPeg . get ( ) . keyBackupKeyFromRecoveryKey = ( ) => 'a raw key' ;
54
60
MatrixClientPeg . get ( ) . checkSecretStorageKey = ( ) => true ;
55
- testInstance . getInstance ( ) . onRecoveryKeyChange ( e ) ;
61
+
62
+ const v = "asdf" ;
63
+ const e = { target : { value : v } } ;
64
+ act ( ( ) => {
65
+ findById ( wrapper , 'mx_securityKey' ) . find ( 'input' ) . simulate ( 'change' , e ) ;
66
+ } ) ;
56
67
// force a validation now because it debounces
57
- await testInstance . getInstance ( ) . validateRecoveryKey ( ) ;
58
- const { recoveryKeyValid } = testInstance . getInstance ( ) . state ;
68
+ await wrapper . instance ( ) . validateRecoveryKey ( ) ;
69
+ const { recoveryKeyValid } = wrapper . instance ( ) . state ;
59
70
expect ( recoveryKeyValid ) . toBe ( true ) ;
60
71
} ) ;
61
72
62
73
it ( "Notifies the user if they input an invalid Security Key" , async function ( ) {
63
- const testInstance = TestRenderer . create (
74
+ const wrapper = mount (
64
75
< AccessSecretStorageDialog
65
76
checkPrivateKey = { async ( ) => false }
66
77
/> ,
@@ -70,22 +81,24 @@ describe("AccessSecretStorageDialog", function() {
70
81
MatrixClientPeg . get ( ) . keyBackupKeyFromRecoveryKey = ( ) => {
71
82
throw new Error ( "that's no key" ) ;
72
83
} ;
73
- testInstance . getInstance ( ) . onRecoveryKeyChange ( e ) ;
84
+
85
+ act ( ( ) => {
86
+ findById ( wrapper , 'mx_securityKey' ) . find ( 'input' ) . simulate ( 'change' , e ) ;
87
+ } ) ;
74
88
// force a validation now because it debounces
75
- await testInstance . getInstance ( ) . validateRecoveryKey ( ) ;
89
+ await wrapper . instance ( ) . validateRecoveryKey ( ) ;
76
90
77
- const { recoveryKeyValid, recoveryKeyCorrect } = testInstance . getInstance ( ) . state ;
91
+ const { recoveryKeyValid, recoveryKeyCorrect } = wrapper . instance ( ) . state ;
78
92
expect ( recoveryKeyValid ) . toBe ( false ) ;
79
93
expect ( recoveryKeyCorrect ) . toBe ( false ) ;
80
- const notification = testInstance . root . findByProps ( {
81
- className : "mx_AccessSecretStorageDialog_recoveryKeyFeedback " +
82
- "mx_AccessSecretStorageDialog_recoveryKeyFeedback_invalid" ,
83
- } ) ;
84
- expect ( notification . props . children ) . toEqual ( "Invalid Security Key" ) ;
94
+
95
+ wrapper . setProps ( { } ) ;
96
+ const notification = wrapper . find ( ".mx_AccessSecretStorageDialog_recoveryKeyFeedback" ) ;
97
+ expect ( notification . props ( ) . children ) . toEqual ( "Invalid Security Key" ) ;
85
98
} ) ;
86
99
87
100
it ( "Notifies the user if they input an invalid passphrase" , async function ( ) {
88
- const testInstance = TestRenderer . create (
101
+ const wrapper = mount (
89
102
< AccessSecretStorageDialog
90
103
checkPrivateKey = { ( ) => false }
91
104
onFinished = { ( ) => { } }
@@ -100,12 +113,12 @@ describe("AccessSecretStorageDialog", function() {
100
113
const e = { target : { value : "a" } } ;
101
114
stubClient ( ) ;
102
115
MatrixClientPeg . get ( ) . isValidRecoveryKey = ( ) => false ;
103
- testInstance . getInstance ( ) . onPassPhraseChange ( e ) ;
104
- await testInstance . getInstance ( ) . onPassPhraseNext ( { preventDefault : ( ) => { } } ) ;
105
- const notification = testInstance . root . findByProps ( {
106
- className : "mx_AccessSecretStorageDialog_keyStatus" ,
107
- } ) ;
108
- expect ( notification . props . children ) . toEqual (
116
+ wrapper . instance ( ) . onPassPhraseChange ( e ) ;
117
+ await wrapper . instance ( ) . onPassPhraseNext ( { preventDefault : ( ) => { } } ) ;
118
+
119
+ wrapper . setProps ( { } ) ;
120
+ const notification = wrapper . find ( ".mx_AccessSecretStorageDialog_keyStatus" ) ;
121
+ expect ( notification . props ( ) . children ) . toEqual (
109
122
[ "\uD83D\uDC4E " , "Unable to access secret storage. Please verify that you " +
110
123
"entered the correct Security Phrase." ] ) ;
111
124
} ) ;
0 commit comments