@@ -6,8 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
6
6
Please see LICENSE files in the repository root for full details.
7
7
*/
8
8
9
- import * as MatrixJs from "matrix-js-sdk/src/matrix" ;
10
9
import { logger } from "matrix-js-sdk/src/logger" ;
10
+ import fetchMockJest from "fetch-mock-jest" ;
11
11
12
12
import { advanceDateAndTime , stubClient } from "./test-utils" ;
13
13
import { IMatrixClientPeg , MatrixClientPeg as peg } from "../src/MatrixClientPeg" ;
@@ -19,14 +19,9 @@ jest.useFakeTimers();
19
19
const PegClass = Object . getPrototypeOf ( peg ) . constructor ;
20
20
21
21
describe ( "MatrixClientPeg" , ( ) => {
22
- let mockClient : MatrixJs . MatrixClient ;
23
-
24
22
beforeEach ( ( ) => {
25
23
// stub out Logger.log which gets called a lot and clutters up the test output
26
24
jest . spyOn ( logger , "log" ) . mockImplementation ( ( ) => { } ) ;
27
-
28
- mockClient = stubClient ( ) ;
29
- jest . spyOn ( MatrixJs , "createClient" ) . mockReturnValue ( mockClient ) ;
30
25
} ) ;
31
26
32
27
afterEach ( ( ) => {
@@ -38,6 +33,7 @@ describe("MatrixClientPeg", () => {
38
33
} ) ;
39
34
40
35
it ( "setJustRegisteredUserId" , ( ) => {
36
+ stubClient ( ) ;
41
37
( peg as any ) . matrixClient = peg . get ( ) ;
42
38
peg . setJustRegisteredUserId ( "@userId:matrix.org" ) ;
43
39
expect ( peg . safeGet ( ) . credentials . userId ) . toBe ( "@userId:matrix.org" ) ;
@@ -56,6 +52,7 @@ describe("MatrixClientPeg", () => {
56
52
} ) ;
57
53
58
54
it ( "setJustRegisteredUserId(null)" , ( ) => {
55
+ stubClient ( ) ;
59
56
( peg as any ) . matrixClient = peg . get ( ) ;
60
57
peg . setJustRegisteredUserId ( null ) ;
61
58
expect ( peg . currentUserIsJustRegistered ( ) ) . toBe ( false ) ;
@@ -74,6 +71,7 @@ describe("MatrixClientPeg", () => {
74
71
beforeEach ( ( ) => {
75
72
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
76
73
testPeg = new PegClass ( ) ;
74
+ fetchMockJest . get ( "http://example.com/_matrix/client/versions" , { } ) ;
77
75
testPeg . replaceUsingCreds ( {
78
76
accessToken : "SEKRET" ,
79
77
homeserverUrl : "http://example.com" ,
@@ -85,20 +83,24 @@ describe("MatrixClientPeg", () => {
85
83
it ( "should initialise the rust crypto library by default" , async ( ) => {
86
84
const mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
87
85
86
+ const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
87
+ const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
88
+
88
89
const cryptoStoreKey = new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) ;
89
90
await testPeg . start ( { rustCryptoStoreKey : cryptoStoreKey } ) ;
90
- expect ( mockClient . initCrypto ) . not . toHaveBeenCalled ( ) ;
91
- expect ( mockClient . initRustCrypto ) . toHaveBeenCalledWith ( { storageKey : cryptoStoreKey } ) ;
91
+ expect ( mockInitCrypto ) . not . toHaveBeenCalled ( ) ;
92
+ expect ( mockInitRustCrypto ) . toHaveBeenCalledWith ( { storageKey : cryptoStoreKey } ) ;
92
93
93
94
// we should have stashed the setting in the settings store
94
95
expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
95
96
} ) ;
96
97
97
98
it ( "Should migrate existing login" , async ( ) => {
98
99
const mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
100
+ const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
99
101
100
102
await testPeg . start ( ) ;
101
- expect ( mockClient . initRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
103
+ expect ( mockInitRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
102
104
103
105
// we should have stashed the setting in the settings store
104
106
expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
0 commit comments