Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/cucumber_tests/shared_steps/equalizerApo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const givenCanWriteToAquaConfig = (given: DefineStepFunction) => {
isGraphViewOn: false,
isCaseSensitiveFs: false,
preAmp: 0,
configFilePath: '',
filters: { unique_id: getDefaultFilterWithId() },
};
const configDirPath = await getConfigPath();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/data/read_only/state.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"isEnabled":true,"isAutoPreAmpOn":true,"isGraphViewOn":true,"isCaseSensitiveFs":false,"preAmp":13,"filters":{"7cf32e8a":{"id":"7cf32e8a","frequency":32,"gain":8,"quality":1.5,"type":"PK"},"3e97b5dc":{"id":"3e97b5dc","frequency":16000,"gain":-10,"quality":2.3,"type":"HSC"}}}
{"isEnabled":true,"isAutoPreAmpOn":true,"isGraphViewOn":true,"isCaseSensitiveFs":false,"configFilePath":"src\\__tests__\\data\\read_only\\config.txt","preAmp":13,"filters":{"7cf32e8a":{"id":"7cf32e8a","frequency":32,"gain":8,"quality":1.5,"type":"PK"},"3e97b5dc":{"id":"3e97b5dc","frequency":16000,"gain":-10,"quality":2.3,"type":"HSC"}}}
2 changes: 1 addition & 1 deletion src/__tests__/data/write/state.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"isEnabled":true,"isAutoPreAmpOn":true,"isGraphViewOn":true,"isCaseSensitiveFs":false,"preAmp":13,"filters":{"7cf32e8a":{"id":"7cf32e8a","frequency":32,"gain":8,"quality":1.5,"type":"PK"},"3e97b5dc":{"id":"3e97b5dc","frequency":16000,"gain":-10,"quality":2.3,"type":"HSC"}}}
{"isEnabled":true,"isAutoPreAmpOn":true,"isGraphViewOn":true,"isCaseSensitiveFs":false,"configFilePath":"src\\__tests__\\data\\read_only\\config.txt","preAmp":13,"filters":{"7cf32e8a":{"id":"7cf32e8a","frequency":32,"gain":8,"quality":1.5,"type":"PK"},"3e97b5dc":{"id":"3e97b5dc","frequency":16000,"gain":-10,"quality":2.3,"type":"HSC"}}}
80 changes: 62 additions & 18 deletions src/__tests__/unit_tests/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,73 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

/* eslint-disable @typescript-eslint/no-unused-vars */
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import App from '../../renderer/App';
import { Channels } from '../../main/api';
import { render, screen } from '@testing-library/react';
import defaultAquaContext from '__tests__/utils/mockAquaProvider';
import {
CONFIG_FILE_PATH_PLACEHOLDER,
ErrorCode,
getErrorDescription,
} from 'common/errors';
import mockElectronAPI from '__tests__/utils/mockElectronAPI';
import { DEFAULT_CONFIG_FILENAME } from 'common/constants';
import App, { AppContent } from '../../renderer/App';
import { AquaProviderWrapper } from '../../renderer/utils/AquaContext';

describe('App', () => {
beforeEach(() => {
Object.defineProperty(window, 'electron', {
get: () => ({
ipcRenderer: {
sendMessage: (_channel: Channels, _args: unknown[]) => {},
on: (_channel: Channels, _func: (...args: unknown[]) => void) => {},
once: (_channel: Channels, _func: (...args: unknown[]) => void) => {},
removeListener: (
_channel: Channels,
_func: (...args: unknown[]) => void
) => {},
closeApp: () => {},
},
}),
});
beforeAll(() => {
mockElectronAPI();
});

it('should render', () => {
expect(render(<App />)).toBeTruthy();
});

describe('AppContent', () => {
const configFilePickerText = 'Select a config file';
it('should render timeout error', () => {
const error = getErrorDescription(ErrorCode.TIMEOUT);
render(
<AquaProviderWrapper
value={{
...defaultAquaContext,
globalError: error,
}}
>
<AppContent />
</AquaProviderWrapper>
);

expect(screen.getByText(error.title)).toBeInTheDocument();
expect(
screen.getByText(`${error.shortError} ${error.action}`)
).toBeInTheDocument();
expect(screen.queryByText(configFilePickerText)).not.toBeInTheDocument();
});

it('should render config file missing error', () => {
const error = getErrorDescription(ErrorCode.CONFIG_NOT_FOUND);
render(
<AquaProviderWrapper
value={{
...defaultAquaContext,
globalError: error,
configFilePath: DEFAULT_CONFIG_FILENAME,
}}
>
<AppContent />
</AquaProviderWrapper>
);

expect(screen.getByText(error.title)).toBeInTheDocument();
expect(
screen.getByText(
`${error.shortError} ${error.action.replace(
CONFIG_FILE_PATH_PLACEHOLDER,
DEFAULT_CONFIG_FILENAME
)}`
)
).toBeInTheDocument();
expect(screen.getByText(configFilePickerText)).toBeInTheDocument();
});
});
});
125 changes: 125 additions & 0 deletions src/__tests__/unit_tests/ConfirmationModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
<AQUA: System-wide parametric audio equalizer interface>
Copyright (C) <2023> <AQUA Dev Team>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import '@testing-library/jest-dom';
import { screen } from '@testing-library/react';
import { setup } from '__tests__/utils/userEventUtils';
import mockElectronAPI from '__tests__/utils/mockElectronAPI';
import ConfirmationModal from 'renderer/widgets/ConfirmationModal';

describe('ConfirmationModal', () => {
const headerText = 'Header';
const bodyText = 'Body';
const retryText = 'Close & Retry';
const exitText = 'Exit';
const onSubmit = jest.fn();
const mockClose = jest.fn();

beforeAll(() => {
mockElectronAPI({ closeApp: mockClose });
});

beforeEach(() => {
mockClose.mockClear();
onSubmit.mockClear();
});

it('should render confirmation modal', async () => {
setup(
<ConfirmationModal
isLoading={false}
headerText={headerText}
bodyText={bodyText}
onSubmit={onSubmit}
/>
);

expect(screen.getByText(headerText)).toBeInTheDocument();
expect(screen.getByText(bodyText)).toBeInTheDocument();
});

it('should be disabled', async () => {
setup(
<ConfirmationModal
isLoading
headerText={headerText}
bodyText={bodyText}
onSubmit={onSubmit}
/>
);

expect(screen.getByText(retryText)).toHaveAttribute(
'aria-disabled',
'true'
);
expect(screen.getByText(exitText)).toHaveAttribute('aria-disabled', 'true');
});

it('should disable retry and enable exit', async () => {
setup(
<ConfirmationModal
isLoading={false}
isSumbitDisabled
headerText={headerText}
bodyText={bodyText}
onSubmit={onSubmit}
/>
);

expect(screen.getByText(retryText)).toHaveAttribute(
'aria-disabled',
'true'
);
expect(screen.getByText(exitText)).not.toHaveAttribute(
'aria-disabled',
'true'
);
});

it('should retry when clicked', async () => {
const { user } = setup(
<ConfirmationModal
isLoading={false}
headerText={headerText}
bodyText={bodyText}
onSubmit={onSubmit}
/>
);

const retryButton = screen.getByText(retryText);
expect(retryButton).toBeInTheDocument();
await user.click(retryButton);
expect(onSubmit).toHaveBeenCalledTimes(1);
});

it('should close when clicked', async () => {
const { user } = setup(
<ConfirmationModal
isLoading={false}
headerText={headerText}
bodyText={bodyText}
onSubmit={onSubmit}
/>
);

const exitButton = screen.getByText(exitText);
expect(exitButton).toBeInTheDocument();
await user.click(exitButton);
expect(mockClose).toHaveBeenCalledTimes(1);
});
});
104 changes: 104 additions & 0 deletions src/__tests__/unit_tests/FilePicker.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
<AQUA: System-wide parametric audio equalizer interface>
Copyright (C) <2023> <AQUA Dev Team>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import '@testing-library/jest-dom';
import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { act } from 'react-test-renderer';
import { setup } from '__tests__/utils/userEventUtils';
import FilePicker from '../../renderer/widgets/FilePicker';

describe('FilePicker', () => {
const labelText = 'Select a file';
const placeholderText = 'No file selected.';
const handleChange = jest.fn();

beforeEach(() => {
handleChange.mockClear();
});

it('should render modal and select a file', async () => {
setup(
<FilePicker
label={labelText}
placeholder={placeholderText}
isDisabled={false}
handleChange={handleChange}
/>
);

expect(screen.getByText(labelText)).toBeInTheDocument();
expect(screen.getByText(placeholderText)).toBeInTheDocument();

const input = screen.getByLabelText(labelText);
const fakeFile = new File(['hello'], 'hello.txt');
await act(async () => {
await waitFor(async () => {
await userEvent.upload(input, fakeFile);
});
});

expect((input as HTMLInputElement).files).toBeDefined();
expect((input as HTMLInputElement).files?.[0]).toStrictEqual(fakeFile);
expect(handleChange).toHaveBeenCalledTimes(1);
});

it('should prevent selecting a file with an invalid file type', async () => {
setup(
<FilePicker
label={labelText}
placeholder={placeholderText}
isDisabled={false}
accept=".png"
handleChange={handleChange}
/>
);

expect(screen.getByText(labelText)).toBeInTheDocument();
expect(screen.getByText(placeholderText)).toBeInTheDocument();

const input = screen.getByLabelText(labelText);
const fakeFile = new File(['hello'], 'hello.txt');
await act(async () => {
await waitFor(async () => {
await userEvent.upload(input, fakeFile);
});
});

expect((input as HTMLInputElement).files).toBeDefined();
expect((input as HTMLInputElement).files?.length).toBe(0);
expect(handleChange).toHaveBeenCalledTimes(0);
});

it('should be disabled', async () => {
setup(
<FilePicker
label={labelText}
placeholder={placeholderText}
isDisabled
handleChange={handleChange}
/>
);

expect(screen.getByText(labelText)).toHaveAttribute(
'aria-disabled',
'true'
);
expect(screen.getByLabelText(labelText)).toBeDisabled();
});
});
Loading