Skip to content

Commit

Permalink
Plugin-helper test migrated from jest to vitest (#2740)
Browse files Browse the repository at this point in the history
  • Loading branch information
abbi4code authored Dec 23, 2024
1 parent 86d59c2 commit 2d7b26f
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import PluginHelper from './Plugin.helper';
import { vi } from 'vitest';

/**
* This file contains unit tests for the PluginHelper component.
*
* The tests cover:
* - Verification that the class contains the required method definitions.
* - Correct functionality of the `generateLinks` method, including returning proper objects.
* - Proper behavior of the `fetchStore` method, including handling of mocked JSON responses.
* - Functionality of the `fetchInstalled` method, verifying it returns the expected JSON data.
*
* These tests use Vitest for test execution and mock the global `fetch` function for asynchronous tests.
*/

describe('Testing src/components/AddOn/support/services/Plugin.helper.ts', () => {
test('Class should contain the required method definitions', () => {
it('Class should contain the required method definitions', () => {
const pluginHelper = new PluginHelper();
expect(pluginHelper).toHaveProperty('fetchStore');
expect(pluginHelper).toHaveProperty('fetchInstalled');
expect(pluginHelper).toHaveProperty('generateLinks');
expect(pluginHelper).toHaveProperty('generateLinks');
});
test('generateLinks should return proper objects', () => {
it('generateLinks should return proper objects', () => {
const obj = {
enabled: true,
name: 'demo',
Expand All @@ -28,9 +41,9 @@ describe('Testing src/components/AddOn/support/services/Plugin.helper.ts', () =>
});
it('fetchStore should return expected JSON', async () => {
const helper = new PluginHelper();
const spy = jest.spyOn(global, 'fetch').mockImplementation(() => {
const spy = vi.spyOn(global, 'fetch').mockImplementation(() => {
const response = new Response();
response.json = jest
response.json = vi
.fn()
.mockReturnValue(Promise.resolve({ data: 'mock data' }));
return Promise.resolve(response);
Expand All @@ -46,11 +59,11 @@ describe('Testing src/components/AddOn/support/services/Plugin.helper.ts', () =>
{ name: 'plugin1', component: 'Component1', enabled: true },
{ name: 'plugin2', component: 'Component2', enabled: false },
];
jest.spyOn(global, 'fetch').mockImplementation(() => {
vi.spyOn(global, 'fetch').mockImplementation(() => {
const response = new Response();
response.json = jest.fn().mockReturnValue(Promise.resolve(mockResponse));
response.json = vi.fn().mockReturnValue(Promise.resolve(mockResponse));
return Promise.resolve(response);
}) as jest.Mock;
});
const result = await pluginHelper.fetchInstalled();
expect(result).toEqual(mockResponse);
});
Expand Down

0 comments on commit 2d7b26f

Please sign in to comment.