Skip to content

Commit

Permalink
increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thecarlo committed Apr 30, 2024
1 parent 601aa88 commit 6a0119b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 5 deletions.
138 changes: 138 additions & 0 deletions src/components/GeneratePassword/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { PasswordMode } from '@enums/passwordMode';
import * as generatePasswordModule from '@functions/generate/randomPassword';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { waitFor } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
Expand Down Expand Up @@ -361,4 +362,141 @@ describe('GeneratePassword', () => {

expect(passwordStrengthIndicatorSecondChild).toHaveClass('bg-green-500');
});

it('should copy an empty string if the password content is null or undefined', async () => {
const mockClipboard = {
writeText: vi.fn(),
};

Object.defineProperty(navigator, 'clipboard', {
value: mockClipboard,
writable: true,
});

render(
<GeneratePassword
passwordMode={PasswordMode.Password}
uppercase={true}
lowercase={true}
numbers={true}
symbols={true}
/>
);

// Mocking to return null for textContent
const passwordPre = screen.getByTitle('generated password');

Object.defineProperty(passwordPre, 'textContent', {
get: () => null,
});

const copyIcon = screen.getByTitle('Copy Password');

fireEvent.click(copyIcon);

await waitFor(() => {
expect(mockClipboard.writeText).toHaveBeenCalledWith('');
});
});
});

describe('GeneratePassword Error Handling', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('should log "Failed to generate password" when an error occurs during password generation', async () => {
vi.spyOn(generatePasswordModule, 'randomPassword').mockImplementation(
() => {
throw new Error('Simulated error');
}
);

// eslint-disable-next-line @typescript-eslint/no-empty-function
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

render(
<GeneratePassword
passwordMode={PasswordMode.Password}
uppercase={true}
lowercase={true}
numbers={true}
symbols={true}
/>
);

const generateButton = screen.getByRole('button', { name: /Generate/i });

fireEvent.click(generateButton);

expect(consoleSpy).toHaveBeenCalledWith('Failed to generate password');
});

it('should log "Error generating password" when an error occurs during password generation from slider change', async () => {
vi.spyOn(generatePasswordModule, 'randomPassword').mockImplementation(
() => {
throw new Error('Simulated slider error');
}
);

// eslint-disable-next-line @typescript-eslint/no-empty-function
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

render(
<GeneratePassword
passwordMode={PasswordMode.Password}
uppercase={true}
lowercase={true}
numbers={true}
symbols={true}
/>
);

const slider = screen.getByRole('slider') as HTMLInputElement;

fireEvent.change(slider, { target: { value: '20' } });

expect(consoleSpy).toHaveBeenCalledWith('Error generating password');

vi.restoreAllMocks();
});

it('should log "Failed to copy password" when an error occurs during password copy', async () => {
const mockClipboard = {
writeText: vi
.fn()
.mockRejectedValue(new Error('Simulated clipboard error')),
};

Object.defineProperty(navigator, 'clipboard', {
value: mockClipboard,
writable: true,
});

render(
<GeneratePassword
passwordMode={PasswordMode.Password}
uppercase={true}
lowercase={true}
numbers={true}
symbols={true}
/>
);

const passwordPre = screen.getByTitle('generated password');

passwordPre.textContent = 'testPassword123!'; // Ensure there is text to copy

const copyIcon = screen.getByTitle('Copy Password');

fireEvent.click(copyIcon);

const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

await vi.waitFor(() =>
expect(consoleSpy).toHaveBeenCalledWith('Failed to copy password')
);

consoleSpy.mockRestore();
});
});
10 changes: 5 additions & 5 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ export default defineConfig({
coverage: {
clean: true,
provider: 'istanbul',
reporter: ['html', 'text', 'json'],
reporter: ['html', 'text'],
include: ['src/**/*.ts?(x)'],
exclude: ['src/**/*.js?(x)', 'src/index.tsx', 'src/**/*.test.ts?(x)'],
all: true,
reportsDirectory: './coverage',
thresholds: {
autoUpdate: true,
statements: 98.63,
branches: 96.73,
statements: 100,
branches: 98.19,
functions: 100,
lines: 98.6,
lines: 100,
},
},
globals: true,
Expand Down Expand Up @@ -66,4 +66,4 @@ export default defineConfig({
},
],
},
});
});

0 comments on commit 6a0119b

Please # to comment.