|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { render } from '@testing-library/react'; |
| 19 | +import { IMyDevice } from 'matrix-js-sdk/src/matrix'; |
| 20 | + |
| 21 | +import DeviceTile from '../../../../../src/components/views/settings/devices/DeviceTile'; |
| 22 | + |
| 23 | +describe('<DeviceTile />', () => { |
| 24 | + const defaultProps = { |
| 25 | + device: { |
| 26 | + device_id: '123', |
| 27 | + }, |
| 28 | + }; |
| 29 | + const getComponent = (props = {}) => ( |
| 30 | + <DeviceTile {...defaultProps} {...props} /> |
| 31 | + ); |
| 32 | + // 14.03.2022 16:15 |
| 33 | + const now = 1647270879403; |
| 34 | + |
| 35 | + jest.useFakeTimers(); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + jest.setSystemTime(now); |
| 39 | + }); |
| 40 | + |
| 41 | + it('renders a device with no metadata', () => { |
| 42 | + const { container } = render(getComponent()); |
| 43 | + expect(container).toMatchSnapshot(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders display name with a tooltip', () => { |
| 47 | + const device: IMyDevice = { |
| 48 | + device_id: '123', |
| 49 | + display_name: 'My device', |
| 50 | + }; |
| 51 | + const { container } = render(getComponent({ device })); |
| 52 | + expect(container).toMatchSnapshot(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders last seen ip metadata', () => { |
| 56 | + const device: IMyDevice = { |
| 57 | + device_id: '123', |
| 58 | + display_name: 'My device', |
| 59 | + last_seen_ip: '1.2.3.4', |
| 60 | + }; |
| 61 | + const { getByTestId } = render(getComponent({ device })); |
| 62 | + expect(getByTestId('device-metadata-lastSeenIp').textContent).toEqual(device.last_seen_ip); |
| 63 | + }); |
| 64 | + |
| 65 | + it('separates metadata with a dot', () => { |
| 66 | + const device: IMyDevice = { |
| 67 | + device_id: '123', |
| 68 | + last_seen_ip: '1.2.3.4', |
| 69 | + last_seen_ts: now - 60000, |
| 70 | + }; |
| 71 | + const { container } = render(getComponent({ device })); |
| 72 | + expect(container).toMatchSnapshot(); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Last activity', () => { |
| 76 | + const MS_DAY = 24 * 60 * 60 * 1000; |
| 77 | + it('renders with day of week and time when last activity is less than 6 days ago', () => { |
| 78 | + const device: IMyDevice = { |
| 79 | + device_id: '123', |
| 80 | + last_seen_ip: '1.2.3.4', |
| 81 | + last_seen_ts: now - (MS_DAY * 3), |
| 82 | + }; |
| 83 | + const { getByTestId } = render(getComponent({ device })); |
| 84 | + expect(getByTestId('device-metadata-lastActivity').textContent).toEqual('Last activity Fri 15:14'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('renders with month and date when last activity is more than 6 days ago', () => { |
| 88 | + const device: IMyDevice = { |
| 89 | + device_id: '123', |
| 90 | + last_seen_ip: '1.2.3.4', |
| 91 | + last_seen_ts: now - (MS_DAY * 8), |
| 92 | + }; |
| 93 | + const { getByTestId } = render(getComponent({ device })); |
| 94 | + expect(getByTestId('device-metadata-lastActivity').textContent).toEqual('Last activity Mar 6'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('renders with month, date, year when activity is in a different calendar year', () => { |
| 98 | + const device: IMyDevice = { |
| 99 | + device_id: '123', |
| 100 | + last_seen_ip: '1.2.3.4', |
| 101 | + last_seen_ts: new Date('2021-12-29').getTime(), |
| 102 | + }; |
| 103 | + const { getByTestId } = render(getComponent({ device })); |
| 104 | + expect(getByTestId('device-metadata-lastActivity').textContent).toEqual('Last activity Dec 29, 2021'); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments