Skip to content

Commit

Permalink
feat: draft and publish api tests (#20150)
Browse files Browse the repository at this point in the history
* feat: refactor test in transaction util

* fix: remove dp tests

* fix: failing api test

* feat: test structure

* feat: update doc service api tests

* fix: test invalid locale param
  • Loading branch information
Marc-Roig committed May 3, 2024
1 parent 2264b66 commit 43f3d25
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 160 deletions.
83 changes: 40 additions & 43 deletions tests/api/core/strapi/document-service/create.test.api.ts
@@ -1,13 +1,22 @@
import type { Core } from '@strapi/types';
import type { Core, Modules } from '@strapi/types';

import { createTestSetup, destroyTestSetup } from '../../../utils/builder-helper';
import { testInTransaction } from '../../../utils/index';
import resources from './resources/index';
import { ARTICLE_UID, findArticlesDb, AUTHOR_UID } from './utils';

let strapi: Core.Strapi;

const createArticle = async (params: Modules.Documents.ServiceParams['create']) => {
return strapi.documents(ARTICLE_UID).create({ populate: '*', ...params });
};

const createAuthor = async (params: Modules.Documents.ServiceParams['create']) => {
return strapi.documents(AUTHOR_UID).create(params);
};

describe('Document Service', () => {
let testUtils;
let strapi: Core.Strapi;

beforeAll(async () => {
testUtils = await createTestSetup(resources);
Expand All @@ -18,15 +27,26 @@ describe('Document Service', () => {
await destroyTestSetup(testUtils);
});

describe('Creates', () => {
testInTransaction('can create a document', async () => {
const article = await strapi.documents(ARTICLE_UID).create({
data: { title: 'Article' },
});
describe('Create', () => {
testInTransaction('Can create a draft document', async () => {
const data = {
title: 'Article',
comp: {
text: 'comp-1',
},
dz: [
{
__component: 'article.dz-comp',
name: 'dz-comp-1',
},
],
};

const article = await createArticle({ data });

// verify that the returned document was updated
expect(article).toMatchObject({
title: 'Article',
...data,
locale: 'en', // default locale
publishedAt: null, // should be a draft
});
Expand All @@ -36,68 +56,45 @@ describe('Document Service', () => {
expect(articles).toHaveLength(1);
});

testInTransaction('can create document with components', async () => {
const article = await strapi.documents(ARTICLE_UID).create({
data: {
title: 'Article',
comp: {
text: 'comp-1',
},
dz: [
{
__component: 'article.dz-comp',
name: 'dz-comp-1',
},
],
},
populate: ['comp', 'dz'],
testInTransaction('Can create an article in dutch', async () => {
const article = await createArticle({
locale: 'nl',
data: { title: 'Article' },
});

// verify that the returned document was updated
expect(article).toMatchObject({
title: 'Article',
comp: {
text: 'comp-1',
},
dz: [
{
__component: 'article.dz-comp',
name: 'dz-comp-1',
},
],
locale: 'nl', // selected locale
publishedAt: null, // should be a draft
});
});

testInTransaction('can create an article in dutch', async () => {
const article = await strapi.documents(ARTICLE_UID).create({
locale: 'nl',
testInTransaction('Can draft and publish', async () => {
const article = await createArticle({
data: { title: 'Article' },
status: 'published',
});

// verify that the returned document was updated
expect(article).toMatchObject({
title: 'Article',
locale: 'nl', // selected locale
publishedAt: null, // should be a draft
publishedAt: expect.any(String),
});
});

testInTransaction.todo('can not directly create a published document');

testInTransaction('publishedAt attribute is ignored when creating document', async () => {
const article = await strapi.documents(ARTICLE_UID).create({
const article = await createArticle({
data: { title: 'Article', publishedAt: new Date() },
});

// verify that the returned document was updated
expect(article).toMatchObject({
title: 'Article',
publishedAt: null, // should be a draft
});
});

testInTransaction('ignores locale parameter on non-localized content type', async () => {
const author = await strapi.documents(AUTHOR_UID).create({
const author = await createAuthor({
// Should be ignored on non-localized content types
locale: 'nl',
data: { name: 'Author' },
Expand Down
8 changes: 4 additions & 4 deletions tests/api/core/strapi/document-service/delete.test.api.ts
Expand Up @@ -19,7 +19,7 @@ describe('Document Service', () => {
});

describe('Delete', () => {
testInTransaction('delete an entire document', async () => {
testInTransaction('Can delete an entire document', async () => {
const articleDb = await findArticleDb({ title: 'Article1-Draft-EN' });
await strapi.documents(ARTICLE_UID).delete({ documentId: articleDb.documentId, locale: '*' });

Expand All @@ -28,7 +28,7 @@ describe('Document Service', () => {
expect(articles).toHaveLength(0);
});

testInTransaction('delete a document with a component', async (trx: any) => {
testInTransaction('Can delete a document with a component', async (trx: any) => {
const componentData = {
comp: {
text: 'comp-1',
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('Document Service', () => {
expect(dz).toBeUndefined();
});

testInTransaction('delete a document locale', async () => {
testInTransaction('Can delete a single document locale', async () => {
const articleDb = await findArticleDb({ title: 'Article1-Draft-NL' });
await strapi.documents(ARTICLE_UID).delete({
documentId: articleDb.documentId,
Expand All @@ -94,7 +94,7 @@ describe('Document Service', () => {
});
});

testInTransaction('status is ignored when deleting a document', async () => {
testInTransaction('Status is ignored when deleting a document', async () => {
const articleDb = await findArticleDb({ title: 'Article2-Draft-EN' });
await strapi.documents(ARTICLE_UID).delete({
documentId: articleDb.documentId,
Expand Down
51 changes: 26 additions & 25 deletions tests/api/core/strapi/document-service/find-many.test.api.ts
@@ -1,12 +1,17 @@
import type { Core } from '@strapi/types';
import type { Core, Modules } from '@strapi/types';

import { createTestSetup, destroyTestSetup } from '../../../utils/builder-helper';
import resources from './resources/index';
import { findArticlesDb } from './utils';
import { ARTICLE_UID, findArticlesDb } from './utils';

let strapi: Core.Strapi;

const findArticles = async (params: Modules.Documents.ServiceParams['findMany']) => {
return strapi.documents(ARTICLE_UID).findMany({ ...params });
};

describe('Document Service', () => {
let testUtils;
let strapi: Core.Strapi;

beforeAll(async () => {
testUtils = await createTestSetup(resources);
Expand All @@ -18,66 +23,62 @@ describe('Document Service', () => {
});

describe('FindMany and Count', () => {
it('find many documents should only return drafts by default', async () => {
const params = { populate: '*' } as const;
const articles = await strapi.documents('api::article.article').findMany(params);
it('Find many documents should only return drafts by default', async () => {
const articles = await findArticles({ populate: '*' });

articles.forEach((article) => {
expect(article.publishedAt).toBe(null);
});

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count({});
expect(count).toBe(articles.length);
});

it('find documents by name returns default locale and draft version', async () => {
it('Find documents by name returns default locale and draft version', async () => {
const params = {
filters: { title: 'Article1-Draft-EN' },
populate: '*',
} as const;

const articlesDb = await findArticlesDb(params.filters);

const articles = await strapi.documents('api::article.article').findMany(params);
const articles = await findArticles(params);

// Should return default language (en) and draft version
expect(articles.length).toBe(1);
expect(articles).toMatchObject(articlesDb);

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count(params);
expect(count).toBe(articles.length);
});

it('find documents by name and locale', async () => {
it('Find documents by name and locale', async () => {
const params = {
locale: 'nl',
filters: { title: 'Article1-Draft-NL' },
populate: '*',
} as const;

// There should not be a nl article called Article1-Draft-EN
const articles = await strapi.documents('api::article.article').findMany(params);
const articles = await findArticles(params);

// Should return dutch locale and draft version
expect(articles.length).toBe(1);

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count(params);
expect(count).toBe(articles.length);
});

it('find dutch documents', async () => {
it('Find dutch documents', async () => {
const params = {
locale: 'nl',
status: 'draft', // 'published' | 'draft'
} as const;

// TODO: compare this with results from findMany
const articlesDb = await findArticlesDb({ title: 'Article1-Draft-EN' });

const articles = await strapi.documents('api::article.article').findMany(params);
const articles = await findArticles(params);

// Should return default language (en) and draft version
expect(articles.length).toBeGreaterThan(0);
Expand All @@ -88,16 +89,16 @@ describe('Document Service', () => {
});

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count(params);
expect(count).toBe(articles.length);
});

it('find published documents', async () => {
it('Find published documents', async () => {
const params = {
status: 'published',
} as const;

const articles = await strapi.documents('api::article.article').findMany(params);
const articles = await findArticles(params);

// Should return default language (en) and draft version
expect(articles.length).toBeGreaterThan(0);
Expand All @@ -107,16 +108,16 @@ describe('Document Service', () => {
});

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count(params);
expect(count).toBe(articles.length);
});

it('find draft documents', async () => {
it('Find draft documents', async () => {
const params = {
status: 'draft',
} as const;

const articles = await strapi.documents('api::article.article').findMany(params);
const articles = await findArticles(params);

// Should return default language (en) and draft version
expect(articles.length).toBeGreaterThan(0);
Expand All @@ -126,7 +127,7 @@ describe('Document Service', () => {
});

// expect count to be the same as findMany
const count = await strapi.documents('api::article.article').count(params);
const count = await strapi.documents(ARTICLE_UID).count(params);
expect(count).toBe(articles.length);
});

Expand Down

0 comments on commit 43f3d25

Please sign in to comment.