From 2538d04bf25cb4a7f74a060d7e7dcbcb11054076 Mon Sep 17 00:00:00 2001 From: Blaine Jester Date: Fri, 17 Nov 2023 11:11:05 -0800 Subject: [PATCH] Fix jest tests --- .../__test__/SelectDeviceForm.spec.js | 32 ++++++++++++++++++- .../sync/SelectDeviceModalGroup/useDevices.js | 9 +++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/__test__/SelectDeviceForm.spec.js b/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/__test__/SelectDeviceForm.spec.js index 59e74b4cd83..1344ddd322f 100644 --- a/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/__test__/SelectDeviceForm.spec.js +++ b/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/__test__/SelectDeviceForm.spec.js @@ -1,6 +1,7 @@ import { shallowMount } from '@vue/test-utils'; import SelectDeviceForm from '../SelectDeviceForm'; import { fetchDevices, updateConnectionStatus } from '../api'; +import { ConnectionStatus } from '../constants'; jest.mock('../api.js', () => ({ fetchDevices: jest.fn(), @@ -13,25 +14,40 @@ const devices = [ id: '1', instance_id: '1', nickname: 'Available Server', + device_name: 'Available Server', base_url: 'http://localhost:8000', application: 'kolibri', available: true, + is_local: true, + since_last_accessed: 0, + subset_of_users_device: false, + kolibri_version: '0.16.0', }, { id: '2', instance_id: '2', nickname: 'Unavailable Server', + device_name: 'Unavailable Server', base_url: 'http://localhost:8001', application: 'kolibri', available: false, + is_local: true, + since_last_accessed: 0, + subset_of_users_device: false, + kolibri_version: '0.16.0', }, { id: '3', instance_id: '3', nickname: 'Content-less Server', + device_name: 'Content-less Server', base_url: 'http://localhost:8001', application: 'kolibri', available: true, + is_local: true, + since_last_accessed: 0, + subset_of_users_device: false, + kolibri_version: '0.16.0', }, ]; @@ -52,9 +68,22 @@ function makeWrapper() { } describe('SelectDeviceForm', () => { + let devices = []; beforeEach(() => { + devices = staticDevices.concat(dynamicDevices); fetchDevices.mockReset(); - fetchDevices.mockResolvedValue(staticDevices.concat(dynamicDevices)); + fetchDevices.mockResolvedValue(devices); + updateConnectionStatus.mockImplementation(device => { + const updatedDevice = devices.find(d => d.id === device.id); + if (!updatedDevice) { + return Promise.resolve({ + ...device, + available: false, + connection_status: ConnectionStatus.Unknown, + }); + } + return Promise.resolve(device); + }); }); it('shows one device for each one fetched', async () => { @@ -89,6 +118,7 @@ describe('SelectDeviceForm', () => { .at(n) .props().disabled; } + await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick(); diff --git a/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/useDevices.js b/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/useDevices.js index 98ae988fb16..efd09c4fa21 100644 --- a/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/useDevices.js +++ b/kolibri/core/assets/src/views/sync/SelectDeviceModalGroup/useDevices.js @@ -1,3 +1,4 @@ +import logger from 'kolibri.lib.logging'; import isArray from 'lodash/isArray'; import { ref, reactive, computed, onBeforeUnmount, watch } from 'kolibri.lib.vueCompositionApi'; import { get, set, useMemoize, useTimeoutPoll } from '@vueuse/core'; @@ -5,6 +6,8 @@ import { get, set, useMemoize, useTimeoutPoll } from '@vueuse/core'; import useMinimumKolibriVersion from 'kolibri.coreVue.composables.useMinimumKolibriVersion'; import { fetchDevices, channelIsAvailableAtDevice, deviceHasMatchingFacility } from './api'; +const logging = logger.getLogger(__filename); + /** * @param {{}} apiParams * @return {{ @@ -105,6 +108,7 @@ export function useDevicesWithFilter(apiParams, filterFunctionOrFunctions) { } } } catch (e) { + logging.error(e); failed = true; } }) @@ -123,7 +127,10 @@ export function useDevicesWithFilter(apiParams, filterFunctionOrFunctions) { .map(d => { // set unavailable if we haven't determined if it should be filtered out yet if (!get(availableIds).includes(d.id)) { - d.available = false; + return { + ...d, + available: false, + }; } return d; });