From 5ade461ea55c6f247b6d810a2e4fa38dcdb663ee Mon Sep 17 00:00:00 2001 From: Kerry Date: Fri, 27 May 2022 15:09:27 +0200 Subject: [PATCH] unit test getVectorConfig (#22373) * test getconfig Signed-off-by: Kerry Archibald * whitespace Signed-off-by: Kerry Archibald --- test/unit-tests/vector/getconfig-test.ts | 122 ++++++++++++++++++ .../vector/platform/WebPlatform-test.ts | 2 - 2 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 test/unit-tests/vector/getconfig-test.ts diff --git a/test/unit-tests/vector/getconfig-test.ts b/test/unit-tests/vector/getconfig-test.ts new file mode 100644 index 0000000000..87de15a7d6 --- /dev/null +++ b/test/unit-tests/vector/getconfig-test.ts @@ -0,0 +1,122 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import request from 'browser-request'; + +import { getVectorConfig } from "../../../src/vector/getconfig"; + +describe('getVectorConfig()', () => { + const setRequestMockImplementationOnce = (err?: unknown, response?: { status: number }, body?: string) => + request.mockImplementationOnce((_opts, callback) => callback(err, response, body)); + + const prevDocumentDomain = document.domain; + const elementDomain = 'app.element.io'; + const now = 1234567890; + const specificConfig = { + brand: 'specific', + } + const generalConfig = { + brand: 'general', + } + + beforeEach(() => { + document.domain = elementDomain; + + // stable value for cachebuster + jest.spyOn(Date, 'now').mockReturnValue(now); + jest.clearAllMocks(); + }); + + afterAll(() => { + document.domain = prevDocumentDomain; + jest.spyOn(Date, 'now').mockRestore(); + }); + + it('requests specific config for document domain', async () => { + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(specificConfig)) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + await getVectorConfig(); + + expect(request.mock.calls[0][0]).toEqual({ method: "GET", url: 'config.app.element.io.json', qs: { cachebuster: now } }) + }); + + it('adds trailing slash to relativeLocation when not an empty string', async () => { + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(specificConfig)) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + await getVectorConfig('..'); + + expect(request.mock.calls[0][0]).toEqual(expect.objectContaining({ url: '../config.app.element.io.json' })) + expect(request.mock.calls[1][0]).toEqual(expect.objectContaining({ url: '../config.json' })) + }); + + it('returns parsed specific config when it is non-empty', async () => { + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(specificConfig)) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(specificConfig); + }); + + it('returns general config when specific config succeeds but is empty', async () => { + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify({})) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(generalConfig); + }); + + it('returns general config when specific config 404s', async () => { + setRequestMockImplementationOnce(undefined, { status: 404 }) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(generalConfig); + }); + + it('returns general config when specific config is fetched from a file and is empty', async () => { + setRequestMockImplementationOnce(undefined, { status: 0 }, '') + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(generalConfig); + }); + + it('returns general config when specific config returns a non-200 status', async () => { + setRequestMockImplementationOnce(undefined, { status: 401 }) + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(generalConfig); + }); + + it('returns general config when specific config returns an error', async () => { + setRequestMockImplementationOnce('err1') + setRequestMockImplementationOnce(undefined, { status: 200 }, JSON.stringify(generalConfig)) + + const result = await getVectorConfig(); + expect(result).toEqual(generalConfig); + }); + + it('rejects with an error when general config rejects', async () => { + setRequestMockImplementationOnce('err-specific'); + setRequestMockImplementationOnce('err-general'); + + await expect(() => getVectorConfig()).rejects.toEqual({"err": "err-general", "response": undefined}); + }); + +}); diff --git a/test/unit-tests/vector/platform/WebPlatform-test.ts b/test/unit-tests/vector/platform/WebPlatform-test.ts index 0dc30147b4..fa5c3e7d8b 100644 --- a/test/unit-tests/vector/platform/WebPlatform-test.ts +++ b/test/unit-tests/vector/platform/WebPlatform-test.ts @@ -23,10 +23,8 @@ import WebPlatform from '../../../../src/vector/platform/WebPlatform'; describe('WebPlatform', () => { beforeEach(() => { jest.clearAllMocks(); - }); - it('returns human readable name', () => { const platform = new WebPlatform(); expect(platform.getHumanReadableName()).toEqual('Web Platform');