@websolutespa/payload-plugin-bowl
Version:
Bowl PayloadCms plugin of the BOM Repository
240 lines (239 loc) • 10.3 kB
JavaScript
import { config, getTestConfig, StaticCollection, StaticData } from '@/test';
import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload';
import * as qs from 'qs-esm';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { staticCollections } from './static.service';
const mockLoader = async ()=>{
return Promise.resolve([
...StaticData
]);
};
describe('static.service', ()=>{
let payload;
let client;
let userToken;
let endUserToken;
let apiKey;
let testConfig;
beforeAll(async ()=>{
const context = await getContext(config);
payload = context.payload;
client = context.client;
userToken = await client.getToken('users', users.admin);
endUserToken = await client.getToken('end_users', endUsers.user);
apiKey = users.api.apiKey;
testConfig = await getTestConfig(client, userToken);
// Register the static collection loader
staticCollections.push(StaticCollection.slug);
});
afterAll(async ()=>{
const index = staticCollections.indexOf(StaticCollection.slug);
if (index > -1) {
staticCollections.splice(index, 1);
}
await clearContext();
});
describe('static collection REST endpoints', ()=>{
describe('Static collection index GET', ()=>{
it('should return all static items without pagination', async ()=>{
const response = await client.get(`/${StaticCollection.slug}?pagination=false&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(Array.isArray(response)).toBe(true);
expect(response).toHaveLength(3);
expect(response[0]).toMatchObject({
id: 'item-1',
name: 'Test Item 1'
});
});
it('should return paginated static items', async ()=>{
const response = await client.get(`/${StaticCollection.slug}?pagination=true&page=1&limit=2&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(response).toHaveProperty('docs');
expect(response).toHaveProperty('totalDocs');
expect(response).toHaveProperty('limit');
expect(response).toHaveProperty('page');
expect(response.docs).toHaveLength(2);
expect(response.totalDocs).toBe(3);
});
it('should filter static items by where clause', async ()=>{
const whereClause = qs.stringify({
where: {
category: {
equals: 'category-a'
}
}
}, {
addQueryPrefix: true
});
const response = await client.get(`/${StaticCollection.slug}${whereClause}&pagination=false&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(Array.isArray(response)).toBe(true);
expect(response).toHaveLength(2);
response.forEach((item)=>{
expect(item.category).toBe('category-a');
});
});
it('should sort static items', async ()=>{
const response = await client.get(`/${StaticCollection.slug}?sort=-name&pagination=false&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(Array.isArray(response)).toBe(true);
expect(response).toHaveLength(3);
expect(response[0].name).toBe('Test Item 3');
expect(response[1].name).toBe('Test Item 2');
expect(response[2].name).toBe('Test Item 1');
});
it('should use pagination by default with market and locale', async ()=>{
const response = await client.get(`/${StaticCollection.slug}?market=${testConfig.documents.market.id}&locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(Array.isArray(response)).toBe(true);
expect(response).toHaveLength(3);
});
});
describe('Static collection detail GET', ()=>{
it('should return a specific static item by ID', async ()=>{
const response = await client.get(`/${StaticCollection.slug}/item-1?locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(response).toMatchObject({
id: 'item-1',
name: 'Test Item 1',
description: 'Description for item 1',
category: 'category-a'
});
});
it('should return 404 for non-existent static item', async ()=>{
try {
await client.get(`/${StaticCollection.slug}/non-existent-id?locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
// Should not reach here
expect(true).toBe(false);
} catch (error) {
expect(error.status).toBe(404);
}
});
it('should handle localized fields', async ()=>{
const response = await client.get(`/${StaticCollection.slug}/item-2?locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
expect(response).toMatchObject({
id: 'item-2',
name: 'Test Item 2'
});
expect(response).toHaveProperty('name');
expect(response).toHaveProperty('description');
});
});
describe('Error handling', ()=>{
it('should handle missing loader gracefully for index', async ()=>{
try {
await client.get(`/non-existent-static-collection?locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
// Should not reach here
expect(true).toBe(false);
} catch (error) {
expect(error.status).toBeGreaterThanOrEqual(400);
}
});
it('should handle missing loader gracefully for detail', async ()=>{
try {
await client.get(`/non-existent-static-collection/item-1?locale=${testConfig.defaultLocale}`, {
headers: {
Authorization: `users API-Key ${apiKey}`
}
});
// Should not reach here
expect(true).toBe(false);
} catch (error) {
expect(error.status).toBeGreaterThanOrEqual(400);
}
});
});
});
describe('static collection hooks', ()=>{
it('should handle find operation with afterStaticOperationHook', async ()=>{
// This would be tested through the payload operations
const docs = await payload.find({
collection: StaticCollection.slug,
locale: testConfig.defaultLocale,
pagination: false
});
expect(docs).toHaveLength(3);
});
it('should handle findByID operation with afterStaticOperationHook', async ()=>{
const doc = await payload.findByID({
collection: StaticCollection.slug,
id: 'item-1',
locale: testConfig.defaultLocale
});
expect(doc).toMatchObject({
id: 'item-1',
name: 'Test Item 1'
});
});
it('should handle find operation with where filtering', async ()=>{
const docs = await payload.find({
collection: StaticCollection.slug,
where: {
category: {
equals: 'category-b'
}
},
locale: testConfig.defaultLocale,
pagination: false
});
expect(docs).toHaveLength(1);
expect(docs[0]).toMatchObject({
id: 'item-2',
category: 'category-b'
});
});
it('should handle find operation with sorting', async ()=>{
const docs = await payload.find({
collection: StaticCollection.slug,
sort: '-name',
locale: testConfig.defaultLocale,
pagination: false
});
expect(docs).toHaveLength(3);
expect(docs[0].name).toBe('Test Item 3');
});
it('should handle find operation with pagination', async ()=>{
const operation = await payload.find({
collection: StaticCollection.slug,
page: 1,
limit: 2,
locale: testConfig.defaultLocale
});
expect(operation.docs).toHaveLength(2);
expect(operation.totalDocs).toBe(3);
expect(operation.page).toBe(1);
expect(operation.limit).toBe(2);
});
});
});
//# sourceMappingURL=static.service.test.js.map