@@ -6,8 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
66Please see LICENSE files in the repository root for full details.
77*/
88
9+ import * as MatrixJs from "matrix-js-sdk/src/matrix" ;
910import { logger } from "matrix-js-sdk/src/logger" ;
10- import fetchMockJest from "fetch-mock-jest" ;
1111
1212import { advanceDateAndTime , stubClient } from "./test-utils" ;
1313import { IMatrixClientPeg , MatrixClientPeg as peg } from "../src/MatrixClientPeg" ;
@@ -19,9 +19,14 @@ jest.useFakeTimers();
1919const PegClass = Object . getPrototypeOf ( peg ) . constructor ;
2020
2121describe ( "MatrixClientPeg" , ( ) => {
22+ let mockClient : MatrixJs . MatrixClient ;
23+
2224 beforeEach ( ( ) => {
2325 // stub out Logger.log which gets called a lot and clutters up the test output
2426 jest . spyOn ( logger , "log" ) . mockImplementation ( ( ) => { } ) ;
27+
28+ mockClient = stubClient ( ) ;
29+ jest . spyOn ( MatrixJs , "createClient" ) . mockReturnValue ( mockClient ) ;
2530 } ) ;
2631
2732 afterEach ( ( ) => {
@@ -33,7 +38,6 @@ describe("MatrixClientPeg", () => {
3338 } ) ;
3439
3540 it ( "setJustRegisteredUserId" , ( ) => {
36- stubClient ( ) ;
3741 ( peg as any ) . matrixClient = peg . get ( ) ;
3842 peg . setJustRegisteredUserId ( "@userId:matrix.org" ) ;
3943 expect ( peg . safeGet ( ) . credentials . userId ) . toBe ( "@userId:matrix.org" ) ;
@@ -52,7 +56,6 @@ describe("MatrixClientPeg", () => {
5256 } ) ;
5357
5458 it ( "setJustRegisteredUserId(null)" , ( ) => {
55- stubClient ( ) ;
5659 ( peg as any ) . matrixClient = peg . get ( ) ;
5760 peg . setJustRegisteredUserId ( null ) ;
5861 expect ( peg . currentUserIsJustRegistered ( ) ) . toBe ( false ) ;
@@ -71,7 +74,6 @@ describe("MatrixClientPeg", () => {
7174 beforeEach ( ( ) => {
7275 // instantiate a MatrixClientPegClass instance, with a new MatrixClient
7376 testPeg = new PegClass ( ) ;
74- fetchMockJest . get ( "http://example.com/_matrix/client/versions" , { } ) ;
7577 testPeg . replaceUsingCreds ( {
7678 accessToken : "SEKRET" ,
7779 homeserverUrl : "http://example.com" ,
@@ -83,24 +85,20 @@ describe("MatrixClientPeg", () => {
8385 it ( "should initialise the rust crypto library by default" , async ( ) => {
8486 const mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
8587
86- const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
87- const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
88-
8988 const cryptoStoreKey = new Uint8Array ( [ 1 , 2 , 3 , 4 ] ) ;
9089 await testPeg . start ( { rustCryptoStoreKey : cryptoStoreKey } ) ;
91- expect ( mockInitCrypto ) . not . toHaveBeenCalled ( ) ;
92- expect ( mockInitRustCrypto ) . toHaveBeenCalledWith ( { storageKey : cryptoStoreKey } ) ;
90+ expect ( mockClient . initCrypto ) . not . toHaveBeenCalled ( ) ;
91+ expect ( mockClient . initRustCrypto ) . toHaveBeenCalledWith ( { storageKey : cryptoStoreKey } ) ;
9392
9493 // we should have stashed the setting in the settings store
9594 expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
9695 } ) ;
9796
9897 it ( "Should migrate existing login" , async ( ) => {
9998 const mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
100- const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
10199
102100 await testPeg . start ( ) ;
103- expect ( mockInitRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
101+ expect ( mockClient . initRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
104102
105103 // we should have stashed the setting in the settings store
106104 expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
0 commit comments