UNPKG

@websolutespa/payload-plugin-bowl

Version:

Bowl PayloadCms plugin of the BOM Repository

72 lines (71 loc) 3.56 kB
import { config, TranslationCollection, TranslationField } from '@/test'; import { clearContext, endUsers, getContext, users } from '@websolutespa/test/payload'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { options } from '../../options'; import { getTranslation } from './translations'; describe('Translations', ()=>{ let payload; let client; let userToken; let endUserToken; let apiKey; 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; }); afterAll(async ()=>{ await clearContext(); }); describe('Collection config', ()=>{ let collectionConfig; beforeAll(async ()=>{ collectionConfig = payload.config.collections.find((collection)=>collection.slug === TranslationCollection.slug); }); describe('Custom translations (via i18n.resources)', ()=>{ it('labels should be correctly translated', ()=>{ options.locales.forEach((locale)=>{ const code = typeof locale === 'string' ? locale : locale.code; const collection = options.translations[code].collection; const labels = collectionConfig.labels; expect(labels.singular[code]).toEqual(collection.singular[TranslationCollection.slug]); expect(labels.plural[code]).toEqual(collection.plural[TranslationCollection.slug]); }); }); it('fields should be correctly translated', ()=>{ const field = collectionConfig.fields.find((field)=>field.type === 'text' && field.name === TranslationField.name); const label = field.label; options.locales.forEach((locale)=>{ const code = typeof locale === 'string' ? locale : locale.code; expect(label[code]).toEqual(options.translations[code].field[TranslationField.name]); }); }); }); describe('Default translations (defined in tranlsations.ts)', ()=>{ it('fields should be correctly translated', ()=>{ const field = collectionConfig.fields.find((field)=>field.type == 'richText' && field.name === 'description'); const label = field.label; options.locales.forEach((locale)=>{ const code = typeof locale === 'string' ? locale : locale.code; expect(label[code]).toEqual(options.translations[code].field[field.name]); }); }); }); }); describe('getTranslation()', ()=>{ const locale = 'en'; it('should return a translation for a valid path', ()=>{ const translatedValue = getTranslation(`collection.singular.${TranslationCollection.slug}`, '', locale); const collection = options.translations[locale].collection; expect(translatedValue).toEqual(collection.singular[TranslationCollection.slug]); }); it('should return a default value for an invalid path', ()=>{ const translatedValue = getTranslation('collection.singular.undefinedCollection', '', locale); expect(translatedValue).toEqual(''); }); }); }); //# sourceMappingURL=translations.test.js.map