chore: merge from 2.0

This commit is contained in:
Elliot DeNolf
2023-09-14 12:10:36 -04:00
2597 changed files with 96505 additions and 105649 deletions

View File

@@ -1,53 +1,54 @@
import { GraphQLClient } from 'graphql-request';
import { initPayloadTest } from '../helpers/configHelpers';
import payload from '../../src';
import type { LocalizedPost, WithLocalizedRelationship } from './payload-types';
import configPromise, {
localizedPostsSlug,
relationshipLocalizedSlug,
withLocalizedRelSlug,
withRequiredLocalizedFields,
} from './config';
import { GraphQLClient } from 'graphql-request'
import type { Config } from '../../packages/payload/src/config/types'
import type { Where } from '../../packages/payload/src/types'
import type { LocalizedPost, WithLocalizedRelationship } from './payload-types'
import payload from '../../packages/payload/src'
import { initPayloadTest } from '../helpers/configHelpers'
import { arrayCollectionSlug } from './collections/Array'
import configPromise from './config'
import {
defaultLocale,
englishTitle,
localizedPostsSlug,
relationEnglishTitle,
relationEnglishTitle2,
relationSpanishTitle,
relationSpanishTitle2,
relationshipLocalizedSlug,
withLocalizedRelSlug,
withRequiredLocalizedFields,
spanishLocale,
spanishTitle,
} from './shared';
import type { Where } from '../../src/types';
import { arrayCollectionSlug } from './collections/Array';
import type { Config } from '../../src/config/types';
} from './shared'
const collection = localizedPostsSlug;
let config: Config;
const collection = localizedPostsSlug
let config: Config
let serverURL;
let serverURL
describe('Localization', () => {
let post1: LocalizedPost;
let postWithLocalizedData: LocalizedPost;
let post1: LocalizedPost
let postWithLocalizedData: LocalizedPost
beforeAll(async () => {
({ serverURL } = await initPayloadTest({ __dirname, init: { local: false } }));
config = await configPromise;
;({ serverURL } = await initPayloadTest({ __dirname, init: { local: false } }))
config = await configPromise
post1 = await payload.create({
collection,
data: {
title: englishTitle,
},
});
})
postWithLocalizedData = await payload.create({
collection,
data: {
title: englishTitle,
},
});
})
await payload.update({
collection,
@@ -56,14 +57,14 @@ describe('Localization', () => {
data: {
title: spanishTitle,
},
});
});
})
})
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload);
await payload.db.destroy(payload)
}
});
})
describe('localized text', () => {
it('create english', async () => {
@@ -72,9 +73,9 @@ describe('Localization', () => {
where: {
title: { equals: post1.title },
},
});
expect(allDocs.docs).toContainEqual(expect.objectContaining(post1));
});
})
expect(allDocs.docs).toContainEqual(expect.objectContaining(post1))
})
it('add spanish translation', async () => {
const updated = await payload.update({
@@ -84,19 +85,19 @@ describe('Localization', () => {
data: {
title: spanishTitle,
},
});
})
expect(updated.title).toEqual(spanishTitle);
expect(updated.title).toEqual(spanishTitle)
const localized = await payload.findByID({
collection,
id: post1.id,
locale: 'all',
});
})
expect(localized.title.en).toEqual(englishTitle);
expect(localized.title.es).toEqual(spanishTitle);
});
expect(localized.title.en).toEqual(englishTitle)
expect(localized.title.es).toEqual(spanishTitle)
})
it('should fallback to english translation when empty', async () => {
const updated = await payload.update({
@@ -106,29 +107,29 @@ describe('Localization', () => {
data: {
title: '',
},
});
})
expect(updated.title).toEqual(englishTitle);
expect(updated.title).toEqual(englishTitle)
const localizedFallback = await payload.findByID({
collection,
id: post1.id,
locale: 'all',
});
})
expect(localizedFallback.title.en).toEqual(englishTitle);
expect(localizedFallback.title.es).toEqual('');
});
expect(localizedFallback.title.en).toEqual(englishTitle)
expect(localizedFallback.title.es).toEqual('')
})
describe('querying', () => {
let localizedPost: LocalizedPost;
let localizedPost: LocalizedPost
beforeEach(async () => {
const { id } = await payload.create({
collection,
data: {
title: englishTitle,
},
});
})
localizedPost = await payload.update({
collection,
@@ -137,48 +138,48 @@ describe('Localization', () => {
data: {
title: spanishTitle,
},
});
});
})
})
it('unspecified locale returns default', async () => {
const localized = await payload.findByID({
collection,
id: localizedPost.id,
});
})
expect(localized.title).toEqual(englishTitle);
});
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);
});
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);
});
expect(localized.title).toEqual(spanishTitle)
})
it('all locales', async () => {
const localized = await payload.findByID({
collection,
locale: 'all',
id: localizedPost.id,
});
})
expect(localized.title.en).toEqual(englishTitle);
expect(localized.title.es).toEqual(spanishTitle);
});
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({
@@ -188,7 +189,7 @@ describe('Localization', () => {
equals: englishTitle,
},
},
});
})
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id);
});
@@ -202,7 +203,7 @@ describe('Localization', () => {
equals: spanishTitle,
},
},
});
})
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id);
});
@@ -216,7 +217,7 @@ describe('Localization', () => {
equals: spanishTitle,
},
},
});
})
expect(result.docs.map(({ id }) => id)).toContain(localizedPost.id);
});
@@ -224,9 +225,9 @@ describe('Localization', () => {
});
describe('Localized Relationship', () => {
let localizedRelation: LocalizedPost;
let localizedRelation2: LocalizedPost;
let withRelationship: WithLocalizedRelationship;
let localizedRelation: LocalizedPost
let localizedRelation2: LocalizedPost
let withRelationship: WithLocalizedRelationship
beforeAll(async () => {
localizedRelation = await createLocalizedPost({
@@ -234,27 +235,30 @@ describe('Localization', () => {
[defaultLocale]: relationEnglishTitle,
[spanishLocale]: relationSpanishTitle,
},
});
})
localizedRelation2 = await createLocalizedPost({
title: {
[defaultLocale]: relationEnglishTitle2,
[spanishLocale]: relationSpanishTitle2,
},
});
})
withRelationship = await payload.create({
collection: withLocalizedRelSlug,
data: {
localizedRelationship: localizedRelation.id,
localizedRelationHasManyField: [localizedRelation.id, localizedRelation2.id],
localizedRelationMultiRelationTo: { relationTo: localizedPostsSlug, value: localizedRelation.id },
localizedRelationMultiRelationTo: {
relationTo: localizedPostsSlug,
value: localizedRelation.id,
},
localizedRelationMultiRelationToHasMany: [
{ relationTo: localizedPostsSlug, value: localizedRelation.id },
{ relationTo: localizedPostsSlug, value: localizedRelation2.id },
],
},
});
});
})
})
describe('regular relationship', () => {
it('can query localized relationship', async () => {
@@ -265,10 +269,10 @@ describe('Localization', () => {
equals: localizedRelation.title,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
});
expect(result.docs[0].id).toEqual(withRelationship.id)
})
it('specific locale', async () => {
const result = await payload.find({
@@ -279,10 +283,10 @@ describe('Localization', () => {
equals: relationSpanishTitle,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
});
expect(result.docs[0].id).toEqual(withRelationship.id)
})
it('all locales', async () => {
const result = await payload.find({
@@ -293,10 +297,10 @@ describe('Localization', () => {
equals: relationSpanishTitle,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
});
expect(result.docs[0].id).toEqual(withRelationship.id)
})
it('populates relationships with all locales', async () => {
// the relationship fields themselves are localized on this collection
@@ -304,14 +308,14 @@ describe('Localization', () => {
collection: relationshipLocalizedSlug,
locale: 'all',
depth: 1,
});
expect((result.docs[0].relationship as any).en.id).toBeDefined();
expect((result.docs[0].relationshipHasMany as any).en[0].id).toBeDefined();
expect((result.docs[0].relationMultiRelationTo as any).en.value.id).toBeDefined();
expect((result.docs[0].relationMultiRelationToHasMany as any).en[0].value.id).toBeDefined();
expect(result.docs[0].arrayField.en[0].nestedRelation.id).toBeDefined();
});
});
})
expect((result.docs[0].relationship as any).en.id).toBeDefined()
expect((result.docs[0].relationshipHasMany as any).en[0].id).toBeDefined()
expect((result.docs[0].relationMultiRelationTo as any).en.value.id).toBeDefined()
expect((result.docs[0].relationMultiRelationToHasMany as any).en[0].value.id).toBeDefined()
expect(result.docs[0].arrayField.en[0].nestedRelation.id).toBeDefined()
})
})
describe('relationship - hasMany', () => {
it('default locale', async () => {
@@ -322,7 +326,7 @@ describe('Localization', () => {
equals: localizedRelation.title,
},
},
});
})
expect(result.docs.map(({ id }) => id)).toContain(withRelationship.id);
@@ -334,7 +338,7 @@ describe('Localization', () => {
equals: localizedRelation2.title,
},
},
});
})
expect(result2.docs.map(({ id }) => id)).toContain(withRelationship.id);
});
@@ -348,9 +352,9 @@ describe('Localization', () => {
equals: relationSpanishTitle,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
expect(result.docs[0].id).toEqual(withRelationship.id)
// Second relationship
const result2 = await payload.find({
@@ -361,10 +365,10 @@ describe('Localization', () => {
equals: relationSpanishTitle2,
},
},
});
})
expect(result2.docs[0].id).toEqual(withRelationship.id);
});
expect(result2.docs[0].id).toEqual(withRelationship.id)
})
it('relationship population uses locale', async () => {
const result = await payload.findByID({
@@ -372,9 +376,9 @@ describe('Localization', () => {
depth: 1,
id: withRelationship.id,
locale: spanishLocale,
});
expect((result.localizedRelationship as LocalizedPost).title).toEqual(relationSpanishTitle);
});
})
expect((result.localizedRelationship as LocalizedPost).title).toEqual(relationSpanishTitle)
})
it('all locales', async () => {
const queryRelation = (where: Where) => {
@@ -382,14 +386,14 @@ describe('Localization', () => {
collection: withLocalizedRelSlug,
locale: 'all',
where,
});
};
})
}
const result = await queryRelation({
'localizedRelationHasManyField.title.en': {
equals: relationEnglishTitle,
},
});
})
expect(result.docs.map(({ id }) => id)).toContain(withRelationship.id);
@@ -398,7 +402,7 @@ describe('Localization', () => {
'localizedRelationHasManyField.title.es': {
equals: relationSpanishTitle,
},
});
})
expect(result2.docs.map(({ id }) => id)).toContain(withRelationship.id);
@@ -407,7 +411,7 @@ describe('Localization', () => {
'localizedRelationHasManyField.title.en': {
equals: relationEnglishTitle2,
},
});
})
expect(result3.docs.map(({ id }) => id)).toContain(withRelationship.id);
@@ -416,11 +420,11 @@ describe('Localization', () => {
'localizedRelationHasManyField.title.es': {
equals: relationSpanishTitle2,
},
});
})
expect(result4.docs[0].id).toEqual(withRelationship.id);
});
});
expect(result4.docs[0].id).toEqual(withRelationship.id)
})
})
describe('relationTo multi', () => {
it('by id', async () => {
@@ -431,9 +435,9 @@ describe('Localization', () => {
equals: localizedRelation.id,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
expect(result.docs[0].id).toEqual(withRelationship.id)
// Second relationship
const result2 = await payload.find({
@@ -444,11 +448,11 @@ describe('Localization', () => {
equals: localizedRelation.id,
},
},
});
})
expect(result2.docs[0].id).toEqual(withRelationship.id);
});
});
expect(result2.docs[0].id).toEqual(withRelationship.id)
})
})
describe('relationTo multi hasMany', () => {
it('by id', async () => {
@@ -459,9 +463,9 @@ describe('Localization', () => {
equals: localizedRelation.id,
},
},
});
})
expect(result.docs[0].id).toEqual(withRelationship.id);
expect(result.docs[0].id).toEqual(withRelationship.id)
// First relationship - spanish locale
const result2 = await payload.find({
@@ -472,9 +476,9 @@ describe('Localization', () => {
equals: localizedRelation.id,
},
},
});
})
expect(result2.docs[0].id).toEqual(withRelationship.id);
expect(result2.docs[0].id).toEqual(withRelationship.id)
// Second relationship
const result3 = await payload.find({
@@ -484,9 +488,9 @@ describe('Localization', () => {
equals: localizedRelation2.id,
},
},
});
})
expect(result3.docs[0].id).toEqual(withRelationship.id);
expect(result3.docs[0].id).toEqual(withRelationship.id)
// Second relationship - spanish locale
const result4 = await payload.find({
@@ -496,20 +500,20 @@ describe('Localization', () => {
equals: localizedRelation2.id,
},
},
});
})
expect(result4.docs[0].id).toEqual(withRelationship.id);
});
});
});
expect(result4.docs[0].id).toEqual(withRelationship.id)
})
})
})
describe('Localized - arrays with nested localized fields', () => {
it('should allow moving rows and retain existing row locale data', async () => {
const globalArray = await payload.findGlobal({
slug: 'global-array',
});
})
const reversedArrayRows = [...globalArray.array].reverse();
const reversedArrayRows = [...globalArray.array].reverse()
const updatedGlobal = await payload.updateGlobal({
slug: 'global-array',
@@ -517,12 +521,12 @@ describe('Localization', () => {
data: {
array: reversedArrayRows,
},
});
})
expect(updatedGlobal.array[0].text.en).toStrictEqual('test en 2');
expect(updatedGlobal.array[0].text.es).toStrictEqual('test es 2');
});
});
expect(updatedGlobal.array[0].text.en).toStrictEqual('test en 2')
expect(updatedGlobal.array[0].text.es).toStrictEqual('test es 2')
})
})
describe('Localized - required', () => {
it('should update without passing all required fields', async () => {
@@ -537,7 +541,7 @@ describe('Localization', () => {
},
],
},
});
})
await payload.update({
collection: withRequiredLocalizedFields,
@@ -552,7 +556,7 @@ describe('Localization', () => {
},
],
},
});
})
const updatedDoc = await payload.update({
collection: withRequiredLocalizedFields,
@@ -560,26 +564,26 @@ describe('Localization', () => {
data: {
title: 'hello x2',
},
});
})
expect(updatedDoc.layout[0].blockType).toStrictEqual('text');
expect(updatedDoc.layout[0].blockType).toStrictEqual('text')
const spanishDoc = await payload.findByID({
collection: withRequiredLocalizedFields,
id: newDoc.id,
locale: spanishLocale,
});
})
expect(spanishDoc.layout[0].blockType).toStrictEqual('number');
});
});
expect(spanishDoc.layout[0].blockType).toStrictEqual('number')
})
})
describe('Localized - GraphQL', () => {
let token;
let token
it('should allow user to login and retrieve populated localized field', async () => {
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}?locale=en`;
const client = new GraphQLClient(url);
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}?locale=en`
const client = new GraphQLClient(url)
const query = `mutation {
loginUser(email: "dev@payloadcms.com", password: "test") {
@@ -590,21 +594,21 @@ describe('Localization', () => {
}
}
}
}`;
}`
const response = await client.request(query);
const result = response.loginUser;
const response = await client.request(query)
const result = response.loginUser
expect(typeof result.token).toStrictEqual('string');
expect(typeof result.user.relation.title).toStrictEqual('string');
expect(typeof result.token).toStrictEqual('string')
expect(typeof result.user.relation.title).toStrictEqual('string')
token = result.token;
});
token = result.token
})
it('should allow retrieval of populated localized fields within meUser', async () => {
// Defining locale=en in graphQL string should not break JWT strategy
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}?locale=en`;
const client = new GraphQLClient(url);
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}?locale=en`
const client = new GraphQLClient(url)
const query = `query {
meUser {
@@ -615,20 +619,20 @@ describe('Localization', () => {
}
}
}
}`;
}`
const response = await client.request(query, null, {
Authorization: `JWT ${token}`,
});
})
const result = response.meUser;
const result = response.meUser
expect(typeof result.user.relation.title).toStrictEqual('string');
});
expect(typeof result.user.relation.title).toStrictEqual('string')
})
it('should create and update collections', async () => {
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}`;
const client = new GraphQLClient(url);
const url = `${serverURL}${config?.routes?.api}${config?.routes?.graphQL}`
const client = new GraphQLClient(url)
const create = `mutation {
createLocalizedPost(
@@ -640,12 +644,11 @@ describe('Localization', () => {
id
title
}
}`;
}`
const { createLocalizedPost: createResult } = await client.request(create, null, {
Authorization: `JWT ${token}`,
});
})
const update = `mutation {
updateLocalizedPost(
@@ -657,27 +660,27 @@ describe('Localization', () => {
) {
title
}
}`;
}`
const { updateLocalizedPost: updateResult } = await client.request(update, null, {
Authorization: `JWT ${token}`,
});
})
const result = await payload.findByID({
collection: localizedPostsSlug,
id: createResult.id,
locale: 'all',
});
})
expect(createResult.title).toStrictEqual(englishTitle);
expect(updateResult.title).toStrictEqual(spanishTitle);
expect(result.title[defaultLocale]).toStrictEqual(englishTitle);
expect(result.title[spanishLocale]).toStrictEqual(spanishTitle);
});
});
expect(createResult.title).toStrictEqual(englishTitle)
expect(updateResult.title).toStrictEqual(spanishTitle)
expect(result.title[defaultLocale]).toStrictEqual(englishTitle)
expect(result.title[spanishLocale]).toStrictEqual(spanishTitle)
})
})
describe('Localized - Arrays', () => {
let docID;
let docID
beforeAll(async () => {
const englishDoc = await payload.create({
@@ -689,20 +692,20 @@ describe('Localization', () => {
},
],
},
});
})
docID = englishDoc.id;
});
docID = englishDoc.id
})
it('should use default locale as fallback', async () => {
const spanishDoc = await payload.findByID({
locale: spanishLocale,
collection: arrayCollectionSlug,
id: docID,
});
})
expect(spanishDoc.items[0].text).toStrictEqual(englishTitle);
});
expect(spanishDoc.items[0].text).toStrictEqual(englishTitle)
})
it('should use empty array as value', async () => {
const updatedSpanishDoc = await payload.update({
@@ -712,10 +715,10 @@ describe('Localization', () => {
data: {
items: [],
},
});
})
expect(updatedSpanishDoc.items).toStrictEqual([]);
});
expect(updatedSpanishDoc.items).toStrictEqual([])
})
it('should use fallback value if setting null', async () => {
await payload.update({
@@ -725,7 +728,7 @@ describe('Localization', () => {
data: {
items: [],
},
});
})
const updatedSpanishDoc = await payload.update({
collection: arrayCollectionSlug,
@@ -734,28 +737,28 @@ describe('Localization', () => {
data: {
items: null,
},
});
})
// should return the value of the fallback locale
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(updatedSpanishDoc.items[0].text).toStrictEqual(englishTitle);
});
});
});
expect(updatedSpanishDoc.items[0].text).toStrictEqual(englishTitle)
})
})
})
async function createLocalizedPost(data: {
title: {
[defaultLocale]: string;
[spanishLocale]: string;
};
[defaultLocale]: string
[spanishLocale]: string
}
}): Promise<LocalizedPost> {
const localizedRelation = await payload.create({
collection,
data: {
title: data.title.en,
},
});
})
await payload.update({
collection,
@@ -764,7 +767,7 @@ async function createLocalizedPost(data: {
data: {
title: data.title.es,
},
});
})
return localizedRelation;
return localizedRelation
}