translations.e2e-spec.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { LanguageCode, mergeConfig } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  8. import * as DE from './fixtures/i18n/de.json';
  9. import * as EN from './fixtures/i18n/en.json';
  10. import {
  11. CUSTOM_ERROR_MESSAGE_TRANSLATION,
  12. TranslationTestPlugin,
  13. } from './fixtures/test-plugins/translation-test-plugin';
  14. describe('Translation', () => {
  15. const { server, adminClient } = createTestEnvironment(
  16. mergeConfig(testConfig(), {
  17. plugins: [TranslationTestPlugin],
  18. }),
  19. );
  20. beforeAll(async () => {
  21. await server.init({
  22. initialData,
  23. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  24. customerCount: 0,
  25. });
  26. await adminClient.asSuperAdmin();
  27. }, TEST_SETUP_TIMEOUT_MS);
  28. afterAll(async () => {
  29. await server.destroy();
  30. });
  31. describe('translations added manualy', () => {
  32. it('shall receive custom error message', async () => {
  33. const { customErrorMessage } = await adminClient.query(gql(CUSTOM_ERROR));
  34. expect(customErrorMessage.errorCode).toBe('CUSTOM_ERROR');
  35. expect(customErrorMessage.message).toBe(CUSTOM_ERROR_MESSAGE_TRANSLATION);
  36. });
  37. it('shall receive german error message', async () => {
  38. const { customErrorMessage } = await adminClient.query(
  39. gql(CUSTOM_ERROR),
  40. {},
  41. { languageCode: LanguageCode.de },
  42. );
  43. expect(customErrorMessage.errorCode).toBe('CUSTOM_ERROR');
  44. expect(customErrorMessage.message).toBe('DE_' + CUSTOM_ERROR_MESSAGE_TRANSLATION);
  45. });
  46. });
  47. describe('translation added by file', () => {
  48. it('shall receive custom error message', async () => {
  49. const { newErrorMessage } = await adminClient.query(gql(NEW_ERROR));
  50. expect(newErrorMessage.errorCode).toBe('NEW_ERROR');
  51. expect(newErrorMessage.message).toBe(EN.errorResult.NEW_ERROR);
  52. });
  53. it('shall receive german error message', async () => {
  54. const { newErrorMessage } = await adminClient.query(
  55. gql(NEW_ERROR),
  56. {},
  57. { languageCode: LanguageCode.de },
  58. );
  59. expect(newErrorMessage.errorCode).toBe('NEW_ERROR');
  60. expect(newErrorMessage.message).toBe(DE.errorResult.NEW_ERROR);
  61. });
  62. });
  63. });
  64. const CUSTOM_ERROR = `
  65. query CustomError {
  66. customErrorMessage {
  67. ... on ErrorResult {
  68. errorCode
  69. message
  70. }
  71. }
  72. }
  73. `;
  74. const NEW_ERROR = `
  75. query NewError {
  76. newErrorMessage {
  77. ... on ErrorResult {
  78. errorCode
  79. message
  80. }
  81. }
  82. }
  83. `;