test: add localization int tests

This commit is contained in:
Elliot DeNolf
2022-07-16 15:01:31 -07:00
parent 81024e4753
commit 79f3de8042
4 changed files with 235 additions and 53 deletions

View File

@@ -1,46 +1,73 @@
import { mapAsync } from '../../src/utilities/mapAsync';
import { buildConfig } from '../buildConfig';
import { LocalizedPost } from './payload-types';
import { englishTitle, spanishLocale, spanishTitle } from './shared';
export type LocalizedPostAllLocale = LocalizedPost & {
title: {
en?: string;
es?: string;
};
};
export const slug = 'localized-posts';
export interface LocalizedPost {
id: string
title: string,
description: string
}
export default buildConfig({
localization: {
locales: [
'en',
'es',
],
locales: ['en', 'es'],
defaultLocale: 'en',
},
collections: [{
slug,
access: {
read: () => true,
create: () => true,
delete: () => true,
update: () => true,
collections: [
{
slug,
access: {
read: () => true,
create: () => true,
delete: () => true,
update: () => true,
},
fields: [
{
name: 'title',
type: 'text',
localized: true,
},
{
name: 'description',
type: 'text',
},
],
},
fields: [
{
name: 'title',
type: 'text',
localized: true,
},
{
name: 'description',
type: 'text',
},
],
}],
],
onInit: async (payload) => {
const collection = slug;
await payload.create({
collection,
data: {
title: englishTitle,
},
});
const localizedPost = await payload.create<LocalizedPost>({
collection,
data: {
title: englishTitle,
},
});
await payload.update<LocalizedPost>({
collection,
id: localizedPost.id,
locale: spanishLocale,
data: {
title: spanishTitle,
},
});
await mapAsync([...Array(11)], async () => {
await payload.create<LocalizedPost>({
collection: slug,
collection,
data: {
title: 'title',
description: 'description',

View File

@@ -1,21 +1,14 @@
import type { Page } from '@playwright/test';
import { expect, test } from '@playwright/test';
import payload from '../../src';
import type { TypeWithTimestamps } from '../../src/collections/config/types';
import { mapAsync } from '../../src/utilities/mapAsync';
import { AdminUrlUtil } from '../helpers/adminUrlUtil';
import { initPayloadTest } from '../helpers/configHelpers';
import { firstRegister, saveDocAndAssert } from '../helpers';
import type { LocalizedPost } from './config';
import type { LocalizedPost } from './payload-types';
import { slug } from './config';
/**
* TODO: Localization
* - [x] create doc in spanish locale
* - [x] retrieve doc in spanish locale
* - [x] retrieve doc in default locale, check for null fields
* - add translations in english to spanish doc
* - [x] check locale toggle button
*
* Fieldtypes to test: (collections for each field type)
* - localized and non-localized: array, block, group, relationship, text
@@ -23,7 +16,7 @@ import { slug } from './config';
* Repeat above for Globals
*/
const { beforeAll, describe, afterEach } = test;
const { beforeAll, describe } = test;
let url: AdminUrlUtil;
const defaultLocale = 'en';
@@ -41,8 +34,6 @@ describe('Localization', () => {
},
});
await clearDocs(); // Clear any seeded data from onInit
url = new AdminUrlUtil(serverURL, slug);
const context = await browser.newContext();
@@ -51,10 +42,6 @@ describe('Localization', () => {
await firstRegister({ page, serverURL });
});
afterEach(async () => {
await clearDocs();
});
describe('localized text', () => {
test('create english post, switch to spanish', async () => {
await page.goto(url.create);
@@ -108,14 +95,6 @@ describe('Localization', () => {
});
});
async function clearDocs(): Promise<void> {
const allDocs = await payload.find<LocalizedPost>({ collection: slug, limit: 100 });
const ids = allDocs.docs.map((doc) => doc.id);
await mapAsync(ids, async (id) => {
await payload.delete({ collection: slug, id });
});
}
async function fillValues(data: Partial<Omit<LocalizedPost, keyof TypeWithTimestamps>>) {
const { title: titleVal, description: descVal } = data;

View File

@@ -0,0 +1,172 @@
import mongoose from 'mongoose';
import { initPayloadTest } from '../helpers/configHelpers';
import payload from '../../src';
import type { LocalizedPost } from './payload-types';
import type { LocalizedPostAllLocale } from './config';
import config from './config';
import { defaultLocale, englishTitle, spanishLocale, spanishTitle } from './shared';
const collection = config.collections[0]?.slug;
describe('array-update', () => {
beforeAll(async () => {
await initPayloadTest({ __dirname });
});
afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.connection.close();
await payload.mongoMemoryServer.stop();
});
describe('Localization', () => {
let post1: LocalizedPost;
beforeAll(async () => {
post1 = await payload.create({
collection,
data: {
title: englishTitle,
},
});
});
describe('localized text', () => {
it('create english', async () => {
const allDocs = await payload.find<LocalizedPost>({
collection,
where: {
title: { equals: post1.title },
},
});
expect(allDocs.docs).toContainEqual(expect.objectContaining(post1));
});
it('add spanish translation', async () => {
const updated = await payload.update<LocalizedPost>({
collection,
id: post1.id,
locale: spanishLocale,
data: {
title: spanishTitle,
},
});
expect(updated.title).toEqual(spanishTitle);
const localized = await payload.findByID<LocalizedPostAllLocale>({
collection,
id: post1.id,
locale: 'all',
});
expect(localized.title.en).toEqual(englishTitle);
expect(localized.title.es).toEqual(spanishTitle);
});
describe('querying', () => {
let localizedPost: LocalizedPost;
beforeEach(async () => {
const { id } = await payload.create<LocalizedPost>({
collection,
data: {
title: englishTitle,
},
});
localizedPost = await payload.update<LocalizedPost>({
collection,
id,
locale: spanishLocale,
data: {
title: spanishTitle,
},
});
});
it('unspecified locale returns default', async () => {
const localized = await payload.findByID({
collection,
id: localizedPost.id,
});
expect(localized.title).toEqual(englishTitle);
});
it('specific locale - same as default', async () => {
const localized = await payload.findByID({
collection,
locale: defaultLocale,
id: localizedPost.id,
});
expect(localized.title).toEqual(englishTitle);
});
it('specific locale - not default', async () => {
const localized = await payload.findByID({
collection,
locale: spanishLocale,
id: localizedPost.id,
});
expect(localized.title).toEqual(spanishTitle);
});
it('all locales', async () => {
const localized = await payload.findByID<LocalizedPostAllLocale>({
collection,
locale: 'all',
id: localizedPost.id,
});
expect(localized.title.en).toEqual(englishTitle);
expect(localized.title.es).toEqual(spanishTitle);
});
it('by localized field value - default locale', async () => {
const result = await payload.find<LocalizedPost>({
collection,
where: {
title: {
equals: englishTitle,
},
},
});
const doc = result.docs[0];
expect(doc.id).toEqual(localizedPost.id);
});
it('by localized field value - alternate locale', async () => {
const result = await payload.find<LocalizedPost>({
collection,
locale: spanishLocale,
where: {
title: {
equals: spanishTitle,
},
},
});
const doc = result.docs[0];
expect(doc.id).toEqual(localizedPost.id);
});
it('by localized field value - opposite locale???', async () => {
const result = await payload.find<LocalizedPost>({
collection,
locale: 'all',
where: {
'title.es': {
equals: spanishTitle,
},
},
});
const doc = result.docs[0];
expect(doc.id).toEqual(localizedPost.id);
});
});
});
});
});

View File

@@ -0,0 +1,4 @@
export const englishTitle = 'english';
export const spanishTitle = 'spanish';
export const defaultLocale = 'en';
export const spanishLocale = 'es';