Skip to content

Commit

Permalink
test: integrate jest into strapi-plugin-old-slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AliKdhim87 committed Sep 26, 2024
1 parent 4199e56 commit 7972baa
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 11 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
'./packages/strapi-plugin-language/tsconfig.server.json',
'./packages/strapi-plugin-old-slugs/tsconfig.json',
'./packages/strapi-plugin-old-slugs/tsconfig.server.json',
'./packages/strapi-plugin-old-slugs/tsconfig.test.json',
'./packages/strapi-plugin-open-forms-embed/tsconfig.json',
'./packages/strapi-plugin-open-forms-embed/tsconfig.server.json',
'./packages/strapi-plugin-uniform-product-name/tsconfig.json',
Expand Down
20 changes: 20 additions & 0 deletions packages/strapi-plugin-old-slugs/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from 'jest';

const config: Config = {
preset: 'ts-jest',
// to obtain access to the matchers.
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
testEnvironment: 'node',
modulePaths: ['<rootDir>'],
testPathIgnorePatterns: ['/node_modules/', '.tmp', '.cache'],
transform: {
'^.+\\.(ts|tsx)$': [
'ts-jest',
{
tsconfig: 'tsconfig.test.json',
},
],
},
};

export default config;
9 changes: 8 additions & 1 deletion packages/strapi-plugin-old-slugs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
},
"devDependencies": {
"@strapi/typescript-utils": "4.25.11",
"@types/jest": "29.5.12",
"@types/react": "18.2.19",
"@types/react-dom": "18.2.19",
"@types/react-router-dom": "5.3.3",
"jest": "29.7.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-router-dom": "5.3.4",
"rimraf": "6.0.1",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"typescript": "5.0.4"
},
"peerDependencies": {
Expand All @@ -48,7 +53,9 @@
"watch": "tsc -p tsconfig.server.json -w",
"build": "tsc -p tsconfig.server.json",
"clean": "rimraf dist",
"prebuild": "yarn clean"
"prebuild": "yarn clean",
"test": "jest --coverage",
"test:watch": "jest --watch"
},
"license": "EUPL-1.2"
}
114 changes: 114 additions & 0 deletions packages/strapi-plugin-old-slugs/server/bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { Strapi } from '@strapi/strapi';
import plugin from './bootstrap'; // Import your plugin file

const mockStrapi: any = {
db: {
lifecycles: {
subscribe: jest.fn(),
},
query: jest.fn(),
},
config: {
get: jest.fn(),
},
};

describe('strapi-plugin-old-slugs', () => {
beforeEach(() => {
jest.clearAllMocks();
});
afterEach(() => {
jest.clearAllMocks();
});

it('should return early if strapi.db is not defined', async () => {
const invalidStrapi = { ...mockStrapi, db: undefined };
await plugin({ strapi: invalidStrapi as Strapi });
expect(mockStrapi.db?.lifecycles.subscribe).not.toHaveBeenCalled();
});

it('should subscribe to lifecycles if strapi.db is defined', async () => {
await plugin({ strapi: mockStrapi as Strapi });
expect(mockStrapi.db?.lifecycles.subscribe).toHaveBeenCalled();
});
it('should subscribe to lifecycles only once', async () => {
await plugin({ strapi: mockStrapi as Strapi });
expect(mockStrapi.db?.lifecycles.subscribe).toHaveBeenCalledTimes(1);
});
describe('Lifecycle event handling', () => {
let lifecycleCallback: Function;

beforeEach(async () => {
await plugin({ strapi: mockStrapi as Strapi });
lifecycleCallback = (mockStrapi.db?.lifecycles.subscribe as jest.Mock).mock.calls[0][0];
});

it('should return early if event model UID is not in config', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = { model: { uid: 'not.in.config' }, action: 'beforeUpdate' };
await lifecycleCallback(event);
expect(mockStrapi.db?.query).not.toHaveBeenCalled();
});
it('should return early if event action is not "beforeUpdate"', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = { model: { uid: 'test.model' }, action: 'beforeCreate' };
await lifecycleCallback(event);
expect(mockStrapi.db?.query).not.toHaveBeenCalled();
});
it('should query the database only for "beforeUpdate" events', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = {
model: { uid: 'test.model' },
action: 'beforeUpdate',
params: { data: {} } as any,
};
(mockStrapi.db?.query as jest.Mock).mockReturnValue({
findOne: jest.fn().mockResolvedValue({}),
});
await lifecycleCallback(event);
expect(mockStrapi.db?.query).toHaveBeenCalled();
});
it('should query the database only for provided uid through config', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = { model: { uid: 'test.model' }, action: 'beforeUpdate', params: { data: {} } as any };
(mockStrapi.db?.query as jest.Mock).mockReturnValue({
findOne: jest.fn().mockResolvedValue({}),
});
await lifecycleCallback(event);
expect(mockStrapi.db?.query).toHaveBeenCalledWith('test.model');
});
it('should update oldSlugs when slug changes', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = {
model: { uid: 'test.model' },
action: 'beforeUpdate',
params: { data: { uuid: '123', slug: 'new-slug' } } as any,
};
const existingEntry = { uuid: '123', slug: 'old-slug', oldSlugs: [] };
(mockStrapi.db?.query as jest.Mock).mockReturnValue({
findOne: jest.fn().mockResolvedValue(existingEntry),
});

await lifecycleCallback(event);

expect(event.params.data.oldSlugs).toEqual(['old-slug']);
});

it('should not duplicate oldSlugs', async () => {
mockStrapi.config.get.mockReturnValue({ contentTypes: [{ uid: 'test.model' }] });
const event = {
model: { uid: 'test.model' },
action: 'beforeUpdate',
params: { data: { uuid: '123', slug: 'same-slug' } as any },
};
const existingEntry = { uuid: '123', slug: 'same-slug', oldSlugs: ['old-slug'] };
(mockStrapi.db?.query as jest.Mock).mockReturnValue({
findOne: jest.fn().mockResolvedValue(existingEntry),
});
const product = await mockStrapi.db?.query('test.model').findOne({ where: { uuid: '123' } });
await lifecycleCallback(event);
expect(product?.oldSlugs).toEqual(['old-slug']);
expect(event.params.data.oldSlugs).toBeUndefined();
});
});
});
12 changes: 12 additions & 0 deletions packages/strapi-plugin-old-slugs/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2016",
"module": "ES6",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"strict": true,
"types": ["jest"]
},
"include": ["**/*.test.ts", "./jest.config.ts", "./tests"]
}
Loading

0 comments on commit 7972baa

Please sign in to comment.