Skip to content

Commit

Permalink
[WIP] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fnbellomo authored and selankon committed Jan 20, 2023
1 parent 7b170b6 commit 99447c9
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 77 deletions.
14 changes: 7 additions & 7 deletions plugins/lime-plugin-align/align.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

import AlignPage from './src/alignPage';
import { render } from 'utils/test_utils';
import { screen, fireEvent, act, cleanup } from '@testing-library/preact';
import { render, flushPromises } from 'utils/test_utils';
import { screen, fireEvent, act } from '@testing-library/preact';
import '@testing-library/jest-dom';
import queryCache from 'utils/queryCache';

import { getMeshIfaces, getAssocList } from './src/alignApi';
import { getBatHost, getCommunitySettings} from 'utils/api';
import { getBatHost, getCommunitySettings, getChangesNeedReboot, getSession} from 'utils/api';
import { DEFAULT_COMMUNITY_SETTINGS } from 'utils/constants';

jest.mock('./src/alignApi');
Expand Down Expand Up @@ -47,9 +47,6 @@ async function findNeighbors() {
return neighs.map(n => n.textContent);
}

function flushPromises() {
return new Promise(resolve => setImmediate(resolve));
}
describe('align page', () => {

beforeAll(() => {
Expand All @@ -65,10 +62,13 @@ describe('align page', () => {
getAssocList.mockClear().mockImplementation(mockAssocList);
getBatHost.mockImplementation(mockBatHost);
getCommunitySettings.mockImplementation(async () => DEFAULT_COMMUNITY_SETTINGS);
getChangesNeedReboot.mockImplementation(async () => true);
getSession.mockImplementation(async () => ({
username: "root",
}));
})

afterEach(() => {
cleanup();
act(() => queryCache.clear());
});

Expand Down
28 changes: 20 additions & 8 deletions plugins/lime-plugin-fbw/src/FbwPage.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@

import FbwPage from './FbwPage';
import { fireEvent, screen, within, act, cleanup } from '@testing-library/preact';
import { fireEvent, screen, within, act } from '@testing-library/preact';
import '@testing-library/jest-dom';
import { render, flushPromises } from 'utils/test_utils';
import { AppContextProvider } from 'utils/app.context';
import { createNetwork, setNetwork, scanStart, getStatus, scanStop} from './FbwApi';
import { enableFetchMocks } from 'jest-fetch-mock';
import { getBoardData } from 'utils/api';
import { getBoardData, getSession, getChangesNeedReboot, getCommunitySettings } from 'utils/api';
import waitForExpect from 'wait-for-expect';
import queryCache from 'utils/queryCache';
import { DEFAULT_COMMUNITY_SETTINGS } from 'utils/constants';



Expand Down Expand Up @@ -58,6 +59,11 @@ describe('Fbw Page', () => {
beforeEach(() => {
fetch.resetMocks();
createNetwork.mockImplementation(async () => ({ status: 'done' }));
getCommunitySettings.mockImplementation(async () => DEFAULT_COMMUNITY_SETTINGS);
getChangesNeedReboot.mockImplementation(async () => false);
getSession.mockImplementation(async () => ({
username: "root",
}));
})

beforeAll(() => {
Expand Down Expand Up @@ -92,17 +98,23 @@ describe('Fbw Page', () => {
describe('Fbw Join Network Page', () => {

beforeEach(() => {
getBoardData.mockImplementation(async() => boardData)
getStatus.mockImplementation(async () => allScanCases)
scanStart.mockImplementation(async () => scanActionSuccess);
getCommunitySettings.mockImplementation(async () => DEFAULT_COMMUNITY_SETTINGS);
getChangesNeedReboot.mockImplementation(async () => false);
getSession.mockImplementation(async () => ({
username: "root",
}));
render(<AppContextProvider><FbwPage /></AppContextProvider>);
})

beforeAll(() => {
getBoardData.mockImplementation(async() => boardData)
getStatus.mockImplementation(async () => allScanCases)
scanStart.mockImplementation(async () => scanActionSuccess)
});
// beforeAll(() => {

// });

afterEach(() => {
cleanup();
// cleanup();
act(() => queryCache.clear());
})

Expand Down
26 changes: 14 additions & 12 deletions plugins/lime-plugin-firmware/firmware.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { fireEvent, cleanup, act, screen } from '@testing-library/preact';
import { render } from 'utils/test_utils';
import { fireEvent, cleanup, act, screen, waitFor } from '@testing-library/preact';
import { render, flushPromises } from 'utils/test_utils';
import '@testing-library/jest-dom';
import waitForExpect from 'wait-for-expect';

Expand All @@ -17,13 +17,12 @@ const secureRollbackText =
const noSecureRollbackText =
/this device does not support secure rollback to previous version if something goes wrong/i;

function flushPromises() {
return new Promise(resolve => setImmediate(resolve));
}

async function stepSelectFile(fileName='test.bin') {
const file = new File(["(⌐□_□)"], fileName);
const fileInput = await screen.findByLabelText(/select file/i);
const file = new File(['(⌐□_□)'], fileName);
fireEvent.change(fileInput, {
target: { files: [file] },
});
Object.defineProperty(fileInput, 'files', {
value: [file]
});
Expand All @@ -35,11 +34,11 @@ async function stepSelectFile(fileName='test.bin') {

async function stepSubmit() {
const submitButton = await screen.findByRole('button', {name: /upgrade/i});
fireEvent.submit(submitButton);
fireEvent.click(submitButton);
}

async function triggerUpgrade() {
const file = stepSelectFile();
const file = await stepSelectFile();
await stepSubmit()
return file;
}
Expand Down Expand Up @@ -122,7 +121,7 @@ describe('firmware form', () => {
it('calls uploadFile to upload the file', async () => {
render(<FirmwarePage />);
const file = await triggerUpgrade();
await waitForExpect(() => {
await waitFor(() => {
expect(uploadFile).toBeCalledWith(file);
})
});
Expand All @@ -131,7 +130,7 @@ describe('firmware form', () => {
uploadFile.mockImplementation(async () => '/tmp/some/given/path');
render(<FirmwarePage />);
triggerUpgrade();
await waitForExpect(() => {
await waitFor(() => {
expect(upgradeFirmware).toHaveBeenCalledWith('/tmp/some/given/path');
})
});
Expand Down Expand Up @@ -196,6 +195,9 @@ describe('firmware form', () => {
getNewVersion.mockImplementation(async () => ({
version: 'SomeNewVersionName'
}));
getDownloadStatus.mockImplementation(async () => ({
download_status: 'not-initiated'
}));
render(<FirmwarePage />)
downloadRelease.mockImplementation(() =>
new Promise(res => setTimeout(res, 10))); // let loading enter screen
Expand Down Expand Up @@ -286,7 +288,6 @@ describe('firmware form', () => {
})
});


describe('firmware confirm', () => {
beforeEach(() => {
// Reset default mock implementations
Expand All @@ -296,6 +297,7 @@ describe('firmware confirm', () => {
})));
upgradeConfirm.mockImplementation(jest.fn(async () => true));
upgradeRevert.mockImplementation(jest.fn(async () => true));
getNewVersion.mockImplementation(async () => ({}))
});

afterEach(() => {
Expand Down
44 changes: 19 additions & 25 deletions plugins/lime-plugin-node-admin/src/screens/hostname.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


import { screen, fireEvent, cleanup, act } from '@testing-library/preact';
import { screen, fireEvent, cleanup, act, waitFor } from '@testing-library/preact';
import '@testing-library/jest-dom';
import waitForExpect from 'wait-for-expect';

Expand All @@ -14,6 +14,14 @@ import { changeHostname } from '../nodeAdminApi';
jest.mock("utils/api");
jest.mock('../nodeAdminApi');

const fillAndSubmitForm = async (hostname, expectHostname = undefined) => {
expectHostname = expectHostname === undefined ? hostname : expectHostname;
const input = await screen.findByLabelText(/node name/i);
fireEvent.input(input, { target: { value: hostname } });
expect(input.value).toBe(expectHostname);
fireEvent.click(await screen.findByRole("button", { name: /save/i }));
};

describe('hostname config', () => {
beforeEach(() => {
getBoardData.mockImplementation(async () => ({
Expand All @@ -27,47 +35,33 @@ describe('hostname config', () => {
});

afterEach(() => {
cleanup();
act(() => queryCache.clear());
});

it('lets you change the hostname', async () => {
render(<HostnamePage />);
const input = await screen.findByLabelText("Node Name");
fireEvent.input(input, { target: { value: 'new-hostname' } });
expect(input.value).toBe('new-hostname');
const saveButton = await screen.findByRole('button', { name: /save/i });
fireEvent.click(saveButton);
await waitForExpect(() => {
await fillAndSubmitForm("new-hostname");
await waitFor(() => {
expect(changeHostname).toBeCalledWith('new-hostname');
});
expect(await screen.findByTestId('changes-need-reboot')).toBeVisible();
});

it('shows an error message when hostname length is less than 3', async () => {
render(<HostnamePage />);
const input = await screen.findByLabelText(/node name/i);
fireEvent.input(input, { target: { value: 'fo' } });
expect(input.value).toBe('fo');
const saveButton = await screen.findByRole('button', { name: /save/i });
fireEvent.click(saveButton);
// await screen.findByText(/the name should have at least 3 characters/i)
// expect(
// screen.getByText(/the name should have at least 3 characters/i)
// ).toBeInTheDocument();
await fillAndSubmitForm("fo");
// await waitFor(() => {
// expect(
// screen.getByText(/the name should have at least 3 characters/i)
// ).toBeInTheDocument();
// });
expect(changeHostname).not.toBeCalled();
});

it('slugifies users input', async () => {
render(<HostnamePage />);
const input = await screen.findByLabelText(/node name/i);
fireEvent.input(input, { target: { value: 'foo_foo foo' } });
expect(input.value).toBe('foo-foo-foo');
const saveButton = await screen.findByRole('button', { name: /save/i });
fireEvent.click(saveButton);
await waitForExpect(() => {
await fillAndSubmitForm("foo_foo foo", "foo-foo-foo");
await waitFor(() => {
expect(changeHostname).toBeCalledWith('foo-foo-foo');
});
expect(await screen.findByTestId('changes-need-reboot')).toBeVisible();
});
});
20 changes: 12 additions & 8 deletions plugins/lime-plugin-pirania/src/screens/voucherList.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "selectFilterOption", "findExpectedVouchers"] }] */

import { fireEvent, screen, act, cleanup } from "@testing-library/preact";
import { fireEvent, screen, act, cleanup, waitFor } from "@testing-library/preact";
import { render } from "utils/test_utils";
import "@testing-library/jest-dom";
import waitForExpect from "wait-for-expect";
Expand Down Expand Up @@ -128,15 +128,17 @@ const findExpectedVouchers = async (expectedVouchers) => {
);
for (let i = 0; i < otherVouchers.length; i++) {
const v = otherVouchers[i];
expect(
screen.queryByTestId(`voucher-item-${v.id}`)
).toBeNull();
await waitFor(() => {
expect(
screen.queryByTestId(`voucher-item-${v.id}`)
).toBeNull();
})
}
}

describe("voucher list", () => {
beforeEach(() => {
listVouchers.mockImplementation(async () => vouchers);
listVouchers.mockImplementation(async () => [...vouchers]);
getBoardData.mockImplementation(async () => ({
hostname: 'conteiner'
}));
Expand Down Expand Up @@ -220,9 +222,11 @@ describe("voucher list", () => {
listVouchers.mockImplementation(async () => [...availableVouchers]);
render(<VoucherList />);
await selectFilterOption('Expired');
expect(
screen.getByText("There are no vouchers matching the current criteria")
).toBeInTheDocument();
await waitFor(() => {
expect(
screen.getByText("There are no vouchers matching the current criteria")
).toBeInTheDocument();
})
});

it("shows a text field with label search by", async () => {
Expand Down
Loading

0 comments on commit 99447c9

Please sign in to comment.