Skip to content

Commit

Permalink
fix: tests to use it.each
Browse files Browse the repository at this point in the history
  • Loading branch information
AugmentedMode committed Oct 24, 2024
1 parent 778eb70 commit efe21ca
Showing 1 changed file with 58 additions and 55 deletions.
113 changes: 58 additions & 55 deletions packages/phishing-controller/src/PhishingDetector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,50 +75,50 @@ describe('PhishingDetector', () => {
);
});

it('logs an error when config name is invalid for various invalid inputs', async () => {
const invalidNames = [
undefined,
null,
true,
false,
0,
1,
1.1,
'',
() => {
return { name: 'test', version: 1 };
},
{},
];

for (const mockInvalidName of invalidNames) {
const consoleErrorMock = jest.spyOn(console, 'error');
it.each([
undefined,
null,
true,
false,
0,
1,
1.1,
'',
() => {
return { name: 'test', version: 1 };
},
{},
])('logs an error when config name is %p', async (mockInvalidName) => {
// Mock console.error to track error logs without cluttering test output
const consoleErrorMock = jest.spyOn(console, 'error');

let detector;
let detector;

await withPhishingDetector(
[
{
allowlist: [],
blocklist: ['blocked-by-first.com'],
fuzzylist: [],
// @ts-expect-error testing invalid input
name: mockInvalidName,
tolerance: 2,
version: 1,
},
],
async ({ detector: d }) => {
detector = d;
await withPhishingDetector(
[
{
allowlist: [],
blocklist: ['blocked-by-first.com'],
fuzzylist: [],
// @ts-expect-error Testing invalid input
name: mockInvalidName,
tolerance: 2,
version: 1,
},
);
],
async ({ detector: d }) => {
detector = d;
},
);

expect(detector).toBeDefined();
// Ensure the detector is still defined
expect(detector).toBeDefined();

expect(console.error).toHaveBeenCalled();
// Check that console.error was called (you can further specify the error if needed)
expect(console.error).toHaveBeenCalled();

consoleErrorMock.mockRestore();
}
// Restore the original console.error implementation
consoleErrorMock.mockRestore();
});

it('drops the invalid config and retains the valid config', async () => {
Expand Down Expand Up @@ -197,20 +197,20 @@ describe('PhishingDetector', () => {
consoleErrorMock.mockRestore();
});

it('logs an error when config version is invalid for various invalid inputs', async () => {
const invalidVersions = [
undefined,
null,
true,
false,
'',
() => {
return { name: 'test', version: 1 };
},
{},
];

for (const mockInvalidVersion of invalidVersions) {
it.each([
undefined,
null,
true,
false,
'',
() => {
return { name: 'test', version: 1 };
},
{},
])(
'logs an error when config version is %p',
async (mockInvalidVersion) => {
// Mock console.error to track error logs without cluttering test output
const consoleErrorMock = jest.spyOn(console, 'error');

let detector;
Expand All @@ -223,7 +223,7 @@ describe('PhishingDetector', () => {
fuzzylist: [],
name: 'first',
tolerance: 2,
// @ts-expect-error testing invalid input
// @ts-expect-error Testing invalid input
version: mockInvalidVersion,
},
],
Expand All @@ -232,13 +232,16 @@ describe('PhishingDetector', () => {
},
);

// Ensure the detector is still defined
expect(detector).toBeDefined();

// Check that console.error was called with an error
expect(console.error).toHaveBeenCalledWith(expect.any(Error));

// Restore the original console.error implementation
consoleErrorMock.mockRestore();
}
});
},
);
});

describe('with legacy config', () => {
Expand Down

0 comments on commit efe21ca

Please sign in to comment.