From 3dcd9f77e75093ac161dd323155940da75bc53bc Mon Sep 17 00:00:00 2001 From: Kerry Archibald Date: Fri, 23 Sep 2022 18:17:26 +0200 Subject: [PATCH] tests --- .../settings/devices/DeviceDetails-test.tsx | 1 + .../__snapshots__/DeviceDetails-test.tsx.snap | 26 ++++++++ .../tabs/user/SessionManagerTab-test.tsx | 8 +-- test/utils/device/clientInformation-test.ts | 62 ++++++++++++++++++- 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/test/components/views/settings/devices/DeviceDetails-test.tsx b/test/components/views/settings/devices/DeviceDetails-test.tsx index dad0ce625be4..3fabc4737efd 100644 --- a/test/components/views/settings/devices/DeviceDetails-test.tsx +++ b/test/components/views/settings/devices/DeviceDetails-test.tsx @@ -51,6 +51,7 @@ describe('', () => { display_name: 'My Device', last_seen_ip: '123.456.789', last_seen_ts: now - 60000000, + clientName: 'Element Web', }; const { container } = render(getComponent({ device })); expect(container).toMatchSnapshot(); diff --git a/test/components/views/settings/devices/__snapshots__/DeviceDetails-test.tsx.snap b/test/components/views/settings/devices/__snapshots__/DeviceDetails-test.tsx.snap index a774c8fac54e..68f0bd7d59a7 100644 --- a/test/components/views/settings/devices/__snapshots__/DeviceDetails-test.tsx.snap +++ b/test/components/views/settings/devices/__snapshots__/DeviceDetails-test.tsx.snap @@ -195,6 +195,32 @@ exports[` renders device with metadata 1`] = ` + + + + + + + + + + + + +
+ Application +
+ Name + + Element Web +
', () => { mockClient.getDevices.mockResolvedValue({ devices: [alicesDevice, alicesMobileDevice] }); mockClient.getAccountData.mockImplementation((eventType: string) => { const content = { - name: 'Element Web', - version: '1.2.3', - url: 'test.com', - }; + name: 'Element Web', + version: '1.2.3', + url: 'test.com', + }; return new MatrixEvent({ type: eventType, content, diff --git a/test/utils/device/clientInformation-test.ts b/test/utils/device/clientInformation-test.ts index 0c4a6cf26485..7faf11b5dd34 100644 --- a/test/utils/device/clientInformation-test.ts +++ b/test/utils/device/clientInformation-test.ts @@ -14,9 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ +import { MatrixEvent } from "matrix-js-sdk/src/matrix"; + import BasePlatform from "../../../src/BasePlatform"; import { IConfigOptions } from "../../../src/IConfigOptions"; -import { recordClientInformation } from "../../../src/utils/device/clientInformation"; +import { + getDeviceClientInformation, + recordClientInformation, +} from "../../../src/utils/device/clientInformation"; import { getMockClientWithEventEmitter } from "../../test-utils"; describe('recordClientInformation()', () => { @@ -84,3 +89,58 @@ describe('recordClientInformation()', () => { ); }); }); + +describe('getDeviceClientInformation()', () => { + const deviceId = 'my-device-id'; + + const mockClient = getMockClientWithEventEmitter({ + getAccountData: jest.fn(), + }); + + beforeEach(() => { + jest.resetAllMocks(); + }); + + it('returns an empty object when no event exists for the device', () => { + expect(getDeviceClientInformation(mockClient, deviceId)).toEqual({}); + + expect(mockClient.getAccountData).toHaveBeenCalledWith( + `io.element.matrix-client-information.${deviceId}`, + ); + }); + + it('returns client information for the device', () => { + const eventContent = { + name: 'Element Web', + version: '1.2.3', + url: 'test.com', + }; + const event = new MatrixEvent({ + type: `io.element.matrix-client-information.${deviceId}`, + content: eventContent, + }); + mockClient.getAccountData.mockReturnValue(event); + expect(getDeviceClientInformation(mockClient, deviceId)).toEqual(eventContent); + }); + + it('excludes values with incorrect types', () => { + const eventContent = { + extraField: 'hello', + name: 'Element Web', + // wrong format + version: { value: '1.2.3' }, + url: 'test.com', + }; + const event = new MatrixEvent({ + type: `io.element.matrix-client-information.${deviceId}`, + content: eventContent, + }); + mockClient.getAccountData.mockReturnValue(event); + // invalid fields excluded + expect(getDeviceClientInformation(mockClient, deviceId)).toEqual({ + name: eventContent.name, + url: eventContent.url, + }); + }); +}); +