Skip to content

Commit

Permalink
Fix jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjester committed Nov 17, 2023
1 parent 84c169d commit 2538d04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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',
},
];

Expand All @@ -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 () => {
Expand Down Expand Up @@ -89,6 +118,7 @@ describe('SelectDeviceForm', () => {
.at(n)
.props().disabled;
}

await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
await wrapper.vm.$nextTick();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
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';

import useMinimumKolibriVersion from 'kolibri.coreVue.composables.useMinimumKolibriVersion';
import { fetchDevices, channelIsAvailableAtDevice, deviceHasMatchingFacility } from './api';

const logging = logger.getLogger(__filename);

/**
* @param {{}} apiParams
* @return {{
Expand Down Expand Up @@ -105,6 +108,7 @@ export function useDevicesWithFilter(apiParams, filterFunctionOrFunctions) {
}
}
} catch (e) {
logging.error(e);
failed = true;
}
})
Expand All @@ -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;
});
Expand Down

0 comments on commit 2538d04

Please sign in to comment.