| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- import { mergeConfig, Zone } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import {
- RelationDecoratorTestService,
- RelationsDecoratorTestPlugin,
- } from './fixtures/test-plugins/relations-decorator-test-plugin';
- describe('Relations decorator', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- customFields: {
- Order: [
- {
- name: 'zone',
- entity: Zone,
- type: 'relation',
- },
- ],
- },
- plugins: [RelationsDecoratorTestPlugin],
- }),
- );
- let testService: RelationDecoratorTestService;
- beforeAll(async () => {
- await server.init({
- initialData,
- customerCount: 1,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- });
- await adminClient.asSuperAdmin();
- testService = server.app.get(RelationDecoratorTestService);
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('empty relations', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- orders(options: { take: 5 }) {
- items {
- id
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual([]);
- });
- it('relations specified in query are included', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- orders(options: { take: 5 }) {
- items {
- customer {
- firstName
- }
- lines {
- featuredAsset {
- preview
- }
- }
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual(['customer', 'lines', 'lines.featuredAsset']);
- });
- it('custom field relations are included', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- orders(options: { take: 5 }) {
- items {
- customFields {
- zone {
- id
- }
- }
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual(['customFields.zone']);
- });
- it('relations specified in Calculated decorator are included', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- orders(options: { take: 5 }) {
- items {
- id
- totalQuantity
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual(['lines']);
- });
- it('defaults to a depth of 3', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- orders(options: { take: 5 }) {
- items {
- lines {
- productVariant {
- product {
- featuredAsset {
- preview
- }
- }
- }
- }
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual([
- 'lines',
- 'lines.productVariant',
- 'lines.productVariant.product',
- ]);
- });
- it('manually set depth of 5', async () => {
- testService.reset();
- await shopClient.query(gql`
- {
- ordersWithDepth5(options: { take: 5 }) {
- items {
- lines {
- productVariant {
- product {
- optionGroups {
- options {
- name
- }
- }
- }
- }
- }
- }
- }
- }
- `);
- expect(testService.getRelations()).toEqual([
- 'lines',
- 'lines.productVariant',
- 'lines.productVariant.product',
- 'lines.productVariant.product.optionGroups',
- 'lines.productVariant.product.optionGroups.options',
- ]);
- });
- });
|