Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions packages/core/server/services/__tests__/url-pattern.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Mock @strapi/strapi
jest.mock('@strapi/strapi', () => ({
factories: {
createCoreService: (uid: string, serviceFactory: any) => serviceFactory,
},
}));

import urlPatternService from '../url-pattern';

// Mock the global strapi object
global.strapi = {
contentTypes: {
'api::article.article': {
info: {
pluralName: 'articles',
},
attributes: {
title: {
type: 'string',
},
categories: {
type: 'relation',
relation: 'manyToMany',
target: 'api::category.category',
},
},
},
'api::category.category': {
attributes: {
name: {
type: 'string',
},
},
},
},
config: {
get: (path: string) => {
if (path === 'plugin::webtools') {
return {
slugify: (str: string) => str.toLowerCase().replace(/\s+/g, '-'),
};
}
return {};
},
},
log: {
error: jest.fn(),
},
} as any;

// Mock getPluginService
jest.mock('../../util/getPluginService', () => ({
getPluginService: (name: string) => {
if (name === 'url-pattern') {
return urlPatternService({ strapi: global.strapi });
}
return {};
},
}));

describe('url-pattern service', () => {
const service = urlPatternService({ strapi: global.strapi });

describe('getAllowedFields', () => {
it('should include fields from many-to-many relations with index', () => {
const contentType = global.strapi.contentTypes['api::article.article'];
const allowedFields = ['string'];

const fields = service.getAllowedFields(contentType, allowedFields);

expect(fields).toContain('categories[0].name');
});
});

describe('resolvePattern', () => {
it('should resolve pattern with many-to-many relation using index', () => {
const uid = 'api::article.article';
const entity = {
title: 'My Article',
categories: [
{ name: 'Tech' },
{ name: 'News' },
],
};
const pattern = '/[categories[0].name]/[title]';

const resolvedPath = service.resolvePattern(uid as any, entity, pattern);

expect(resolvedPath).toBe('/tech/my-article');
});

it('should resolve pattern with many-to-many relation using specific index', () => {
const uid = 'api::article.article';
const entity = {
title: 'My Article',
categories: [
{ name: 'Tech' },
{ name: 'News' },
],
};
const pattern = '/[categories[1].name]/[title]';

const resolvedPath = service.resolvePattern(uid as any, entity, pattern);

expect(resolvedPath).toBe('/news/my-article');
});

it('should handle empty many-to-many relation', () => {
const uid = 'api::article.article';
const entity = {
title: 'My Article',
categories: [],
};
const pattern = '/[categories[0].name]/[title]';

const resolvedPath = service.resolvePattern(uid as any, entity, pattern);

expect(resolvedPath).toBe('/my-article');
});
});
});
Loading
Loading