From 3d0045dab51410745b562783d36d5622203d720a Mon Sep 17 00:00:00 2001 From: Kerry Date: Tue, 3 May 2022 17:09:07 +0200 Subject: [PATCH] Test typescriptification - Terms/ScalarAuthClient (#8480) * test/Terms-test.js -> test/Terms-test.tsx Signed-off-by: Kerry Archibald * fix ts issues in Terms-test Signed-off-by: Kerry Archibald * test/ScalarAuthClient-test.js -> test/ScalarAuthClient-test.ts Signed-off-by: Kerry Archibald * ts fixes in ScalarAuthClient-test Signed-off-by: Kerry Archibald * comment Signed-off-by: Kerry Archibald --- ...lient-test.js => ScalarAuthClient-test.ts} | 10 ++- test/{Terms-test.js => Terms-test.tsx} | 84 ++++++++++++------- 2 files changed, 61 insertions(+), 33 deletions(-) rename test/{ScalarAuthClient-test.js => ScalarAuthClient-test.ts} (83%) rename test/{Terms-test.js => Terms-test.tsx} (68%) diff --git a/test/ScalarAuthClient-test.js b/test/ScalarAuthClient-test.ts similarity index 83% rename from test/ScalarAuthClient-test.js rename to test/ScalarAuthClient-test.ts index a597c2cb2e..3b6fcf77b2 100644 --- a/test/ScalarAuthClient-test.js +++ b/test/ScalarAuthClient-test.ts @@ -19,6 +19,8 @@ import { MatrixClientPeg } from '../src/MatrixClientPeg'; import { stubClient } from './test-utils'; describe('ScalarAuthClient', function() { + const apiUrl = 'test.com/api'; + const uiUrl = 'test.com/app'; beforeEach(function() { window.localStorage.getItem = jest.fn((arg) => { if (arg === "mx_scalar_token") return "brokentoken"; @@ -27,15 +29,17 @@ describe('ScalarAuthClient', function() { }); it('should request a new token if the old one fails', async function() { - const sac = new ScalarAuthClient(); + const sac = new ScalarAuthClient(apiUrl, uiUrl); - sac.getAccountName = jest.fn((arg) => { + // @ts-ignore unhappy with Promise calls + jest.spyOn(sac, 'getAccountName').mockImplementation((arg: string) => { switch (arg) { case "brokentoken": return Promise.reject({ message: "Invalid token", }); case "wokentoken": + default: return Promise.resolve(MatrixClientPeg.get().getUserId()); } }); @@ -49,6 +53,8 @@ describe('ScalarAuthClient', function() { await sac.connect(); expect(sac.exchangeForScalarToken).toBeCalledWith('this is your openid token'); + expect(sac.hasCredentials).toBeTruthy(); + // @ts-ignore private property expect(sac.scalarToken).toEqual('wokentoken'); }); }); diff --git a/test/Terms-test.js b/test/Terms-test.tsx similarity index 68% rename from test/Terms-test.js rename to test/Terms-test.tsx index 2bd5b6f43d..7e22731518 100644 --- a/test/Terms-test.js +++ b/test/Terms-test.tsx @@ -14,10 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -import * as Matrix from 'matrix-js-sdk/src/matrix'; +import { + MatrixEvent, + EventType, + SERVICE_TYPES, +} from 'matrix-js-sdk/src/matrix'; import { startTermsFlow, Service } from '../src/Terms'; -import { stubClient } from './test-utils'; +import { getMockClientWithEventEmitter } from './test-utils'; import { MatrixClientPeg } from '../src/MatrixClientPeg'; const POLICY_ONE = { @@ -36,17 +40,31 @@ const POLICY_TWO = { }, }; -const IM_SERVICE_ONE = new Service(Matrix.SERVICE_TYPES.IM, 'https://imone.test', 'a token token'); -const IM_SERVICE_TWO = new Service(Matrix.SERVICE_TYPES.IM, 'https://imtwo.test', 'a token token'); +const IM_SERVICE_ONE = new Service(SERVICE_TYPES.IM, 'https://imone.test', 'a token token'); +const IM_SERVICE_TWO = new Service(SERVICE_TYPES.IM, 'https://imtwo.test', 'a token token'); describe('Terms', function() { + const mockClient = getMockClientWithEventEmitter({ + getAccountData: jest.fn(), + getTerms: jest.fn(), + agreeToTerms: jest.fn(), + setAccountData: jest.fn(), + }); + beforeEach(function() { - stubClient(); + jest.clearAllMocks(); + mockClient.getAccountData.mockReturnValue(null); + mockClient.getTerms.mockResolvedValue(null); + mockClient.setAccountData.mockResolvedValue({}); + }); + + afterAll(() => { + jest.spyOn(MatrixClientPeg, 'get').mockRestore(); }); it('should prompt for all terms & services if no account data', async function() { - MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue(null); - MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({ + mockClient.getAccountData.mockReturnValue(null); + mockClient.getTerms.mockResolvedValue({ policies: { "policy_the_first": POLICY_ONE, }, @@ -65,24 +83,26 @@ describe('Terms', function() { }); it('should not prompt if all policies are signed in account data', async function() { - MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({ - getContent: jest.fn().mockReturnValue({ + const directEvent = new MatrixEvent({ + type: EventType.Direct, + content: { accepted: ["http://example.com/one"], - }), + }, }); - MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({ + mockClient.getAccountData.mockReturnValue(directEvent); + mockClient.getTerms.mockResolvedValue({ policies: { "policy_the_first": POLICY_ONE, }, }); - MatrixClientPeg.get().agreeToTerms = jest.fn(); + mockClient.agreeToTerms; const interactionCallback = jest.fn(); await startTermsFlow([IM_SERVICE_ONE], interactionCallback); expect(interactionCallback).not.toHaveBeenCalled(); - expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith( - Matrix.SERVICE_TYPES.IM, + expect(mockClient.agreeToTerms).toBeCalledWith( + SERVICE_TYPES.IM, 'https://imone.test', 'a token token', ["http://example.com/one"], @@ -90,18 +110,20 @@ describe('Terms', function() { }); it("should prompt for only terms that aren't already signed", async function() { - MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({ - getContent: jest.fn().mockReturnValue({ + const directEvent = new MatrixEvent({ + type: EventType.Direct, + content: { accepted: ["http://example.com/one"], - }), + }, }); - MatrixClientPeg.get().getTerms = jest.fn().mockReturnValue({ + mockClient.getAccountData.mockReturnValue(directEvent); + + mockClient.getTerms.mockResolvedValue({ policies: { "policy_the_first": POLICY_ONE, "policy_the_second": POLICY_TWO, }, }); - MatrixClientPeg.get().agreeToTerms = jest.fn(); const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]); await startTermsFlow([IM_SERVICE_ONE], interactionCallback); @@ -114,8 +136,8 @@ describe('Terms', function() { }, }, ], ["http://example.com/one"]); - expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith( - Matrix.SERVICE_TYPES.IM, + expect(mockClient.agreeToTerms).toBeCalledWith( + SERVICE_TYPES.IM, 'https://imone.test', 'a token token', ["http://example.com/one", "http://example.com/two"], @@ -123,13 +145,15 @@ describe('Terms', function() { }); it("should prompt for only services with un-agreed policies", async function() { - MatrixClientPeg.get().getAccountData = jest.fn().mockReturnValue({ - getContent: jest.fn().mockReturnValue({ + const directEvent = new MatrixEvent({ + type: EventType.Direct, + content: { accepted: ["http://example.com/one"], - }), + }, }); + mockClient.getAccountData.mockReturnValue(directEvent); - MatrixClientPeg.get().getTerms = jest.fn((serviceType, baseUrl, accessToken) => { + mockClient.getTerms.mockImplementation(async (_serviceTypes: SERVICE_TYPES, baseUrl: string) => { switch (baseUrl) { case 'https://imone.test': return { @@ -146,8 +170,6 @@ describe('Terms', function() { } }); - MatrixClientPeg.get().agreeToTerms = jest.fn(); - const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]); await startTermsFlow([IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback); @@ -159,14 +181,14 @@ describe('Terms', function() { }, }, ], ["http://example.com/one"]); - expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith( - Matrix.SERVICE_TYPES.IM, + expect(mockClient.agreeToTerms).toBeCalledWith( + SERVICE_TYPES.IM, 'https://imone.test', 'a token token', ["http://example.com/one"], ); - expect(MatrixClientPeg.get().agreeToTerms).toBeCalledWith( - Matrix.SERVICE_TYPES.IM, + expect(mockClient.agreeToTerms).toBeCalledWith( + SERVICE_TYPES.IM, 'https://imtwo.test', 'a token token', ["http://example.com/two"],