Skip to content

Commit

Permalink
test(frontend): Fix tests again
Browse files Browse the repository at this point in the history
  • Loading branch information
maikbasel committed May 22, 2024
1 parent 5dd1a11 commit 9f90a7c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 50 deletions.
4 changes: 3 additions & 1 deletion src/modules/credentials/application/validateCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CredentialsDataSPI } from '@/modules/credentials/core/domain';
import { Result } from 'oxide.ts';
import { BackendError } from '@/modules/common/error';

export async function validateCredentials(
credentialsDataSPI: CredentialsDataSPI,
profileName: string
) {
): Promise<Result<boolean, BackendError>> {
return credentialsDataSPI.validateCredentials(profileName);
}
49 changes: 36 additions & 13 deletions src/sections/profiles/components/profile-data-table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import { ProfileSet } from '@/modules/profiles/core/domain';
import { ProfileDataTable } from '@/sections/profiles/components/profile-data-table';
import userEvent from '@testing-library/user-event';
import { SWRConfig } from 'swr';
import { mockIPC } from '@tauri-apps/api/mocks';
import { TooltipProvider } from '@/components/ui/tooltip';
import { validateCredentials } from '@/modules/credentials/application/validateCredentials';
import { Ok } from 'oxide.ts';
import { DIContextProvider } from '@/context/di-context';

jest.mock('@/modules/credentials/application/validateCredentials', () => ({
...jest.requireActual(
'@/modules/credentials/application/validateCredentials'
),
validateCredentials: jest.fn(),
}));

const mockValidateCredentials = validateCredentials as jest.MockedFunction<
typeof validateCredentials
>;

describe('<ProfileDataTable />', () => {
const profileSet: ProfileSet = {
Expand All @@ -25,18 +38,16 @@ describe('<ProfileDataTable />', () => {
};

beforeEach(() => {
mockIPC((cmd) => {
if (cmd === 'validate_credentials') {
return false;
}
});
mockValidateCredentials.mockResolvedValue(Ok(false));
});

it('should render profile data table with profile set data', async () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={profileSet} />
<DIContextProvider>
<ProfileDataTable data={profileSet} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand All @@ -54,7 +65,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={profileSet} />
<DIContextProvider>
<ProfileDataTable data={profileSet} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand All @@ -70,7 +83,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={profileSet} />
<DIContextProvider>
<ProfileDataTable data={profileSet} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand All @@ -88,7 +103,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={profileSet} />
<DIContextProvider>
<ProfileDataTable data={profileSet} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand All @@ -103,7 +120,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={profileSet} />
<DIContextProvider>
<ProfileDataTable data={profileSet} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand Down Expand Up @@ -146,7 +165,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={input} />
<DIContextProvider>
<ProfileDataTable data={input} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand Down Expand Up @@ -192,7 +213,9 @@ describe('<ProfileDataTable />', () => {
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<ProfileDataTable data={input} />
<DIContextProvider>
<ProfileDataTable data={input} />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);
Expand Down
85 changes: 49 additions & 36 deletions src/sections/profiles/components/test-credentials-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import TestCredentialsButton from './test-credentials-button';
import { clearMocks, mockIPC } from '@tauri-apps/api/mocks';
import { SWRConfig } from 'swr';
import userEvent from '@testing-library/user-event';
import { TooltipProvider } from '@/components/ui/tooltip';
import { validateCredentials } from '@/modules/credentials/application/validateCredentials';
import { Ok } from 'oxide.ts';
import { DIContextProvider } from '@/context/di-context';

jest.mock('@/modules/credentials/application/validateCredentials', () => ({
...jest.requireActual(
'@/modules/credentials/application/validateCredentials'
),
validateCredentials: jest.fn(),
}));

const mockValidateCredentials = validateCredentials as jest.MockedFunction<
typeof validateCredentials
>;

describe('<TestCredentialsButton />', () => {
afterEach(() => {
clearMocks();
jest.resetAllMocks();
});

it('should render shield alert icon when profile credentials are invalid', async () => {
mockIPC((cmd) => {
if (cmd === 'validate_credentials') {
return false;
}
jest.isolateModules(async () => {
mockValidateCredentials.mockResolvedValue(Ok(false));
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<DIContextProvider>
<TestCredentialsButton profile='prof1' />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);

const button = screen.getByRole('button');
await userEvent.click(button);

const shieldSvg = button.querySelector('svg');
expect(shieldSvg).toHaveClass('lucide-shield-alert');
});
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<TestCredentialsButton profile='prof1' />
</TooltipProvider>
</SWRConfig>
);

const button = screen.getByRole('button');
await userEvent.click(button);

const shieldSvg = button.querySelector('svg');
expect(shieldSvg).toHaveClass('lucide-shield-alert');
});

it('should render shield check icon when profile credentials are valid', async () => {
mockIPC((cmd) => {
if (cmd === 'validate_credentials') {
return true;
}
jest.isolateModules(async () => {
mockValidateCredentials.mockResolvedValue(Ok(true));
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<DIContextProvider>
<TestCredentialsButton profile='prof1' />
</DIContextProvider>
</TooltipProvider>
</SWRConfig>
);

const button = screen.getByRole('button');
await userEvent.click(button);

const shieldSvg = button.querySelector('svg');
expect(shieldSvg).toHaveClass('lucide-shield-check');
});
render(
<SWRConfig value={{ provider: () => new Map() }}>
<TooltipProvider>
<TestCredentialsButton profile='prof1' />
</TooltipProvider>
</SWRConfig>
);

const button = screen.getByRole('button');
await userEvent.click(button);

const shieldSvg = button.querySelector('svg');
expect(shieldSvg).toHaveClass('lucide-shield-check');
});
});

0 comments on commit 9f90a7c

Please sign in to comment.