| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491 |
- import { LogicalOperator } from '@vendure/common/lib/generated-types';
- import { mergeConfig } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
- import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
- import { LanguageCode, SortOrder } from './graphql/generated-e2e-admin-types';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- import { fixPostgresTimezone } from './utils/fix-pg-timezone';
- import { sortById } from './utils/test-order-utils';
- fixPostgresTimezone();
- describe('ListQueryBuilder', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- apiOptions: {
- shopListQueryLimit: 10,
- adminListQueryLimit: 30,
- },
- plugins: [ListQueryPlugin],
- }),
- );
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 3,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- function getItemLabels(items: any[]): string[] {
- return items.map((x: any) => x.label).sort();
- }
- describe('pagination', () => {
- it('all en', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {},
- },
- { languageCode: LanguageCode.en },
- );
- expect(testEntities.totalItems).toBe(6);
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
- expect(testEntities.items.map((i: any) => i.name)).toEqual(expect.arrayContaining([
- 'apple',
- 'bike',
- 'cake',
- 'dog',
- 'egg',
- 'baum', // if default en lang does not exist, use next available lang
- ]));
- });
- it('all de', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {},
- },
- { languageCode: LanguageCode.de },
- );
- expect(testEntities.totalItems).toBe(6);
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
- expect(testEntities.items.map((i: any) => i.name)).toEqual(expect.arrayContaining([
- 'apfel',
- 'fahrrad',
- 'kuchen',
- 'hund',
- 'egg', // falls back to en translation when de doesn't exist
- 'baum',
- ]));
- });
- it('take', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- take: 2,
- },
- });
- expect(testEntities.totalItems).toBe(6);
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
- });
- it('skip', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- skip: 2,
- },
- });
- expect(testEntities.totalItems).toBe(6);
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
- });
- it('skip negative is ignored', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- skip: -1,
- },
- });
- expect(testEntities.totalItems).toBe(6);
- expect(testEntities.items.length).toBe(6);
- });
- it('take zero is ignored', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- take: 0,
- },
- });
- expect(testEntities.totalItems).toBe(6);
- expect(testEntities.items.length).toBe(6);
- });
- it('take negative is ignored', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- take: -1,
- },
- });
- expect(testEntities.totalItems).toBe(6);
- expect(testEntities.items.length).toBe(6);
- });
- it(
- 'take beyond adminListQueryLimit',
- assertThrowsWithMessage(async () => {
- await adminClient.query(GET_LIST, {
- options: {
- take: 50,
- },
- });
- }, 'Cannot take more than 30 results from a list query'),
- );
- it(
- 'take beyond shopListQueryLimit',
- assertThrowsWithMessage(async () => {
- await shopClient.query(GET_LIST, {
- options: {
- take: 50,
- },
- });
- }, 'Cannot take more than 10 results from a list query'),
- );
- });
- describe('string filtering', () => {
- it('eq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- label: {
- eq: 'B',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B']);
- });
- it('notEq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- label: {
- notEq: 'B',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E', 'F']);
- });
- it('contains', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- contains: 'adip',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C']);
- });
- it('notContains', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- notContains: 'te',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
- });
- it('in', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- label: {
- in: ['A', 'C'],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C']);
- });
- it('notIn', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- label: {
- notIn: ['A', 'C'],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E', 'F']);
- });
- it('isNull true', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableString: {
- isNull: true,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
- });
- it('isNull false', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableString: {
- isNull: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
- });
- describe('regex', () => {
- it('simple substring', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'or',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
- });
- it('start of string', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: '^in',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['E']);
- });
- it('end of string', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'or$',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['D']);
- });
- it('alternation', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'dolor|tempor',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
- });
- it('complex', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
- });
- });
- });
- describe('ID filtering', () => {
- it('eq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- eq: 'T_13',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['D']);
- });
- it('notEq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- notEq: 'T_13',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'E', 'F']);
- });
- it('in', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- in: ['T_10', 'T_15'],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'F']);
- });
- it('in with empty set', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- in: [],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual([]);
- });
- it('notIn', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- notIn: ['T_10', 'T_15'],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
- });
- it('notIn with empty set', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- ownerId: {
- notIn: [],
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
- });
- it('isNull true', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableId: {
- isNull: true,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
- });
- it('isNull false', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableId: {
- isNull: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
- });
- describe('regex', () => {
- it('simple substring', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'or',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'D']);
- });
- it('start of string', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: '^in',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['E']);
- });
- it('end of string', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'or$',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['D']);
- });
- it('alternation', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: 'dolor|tempor',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D']);
- });
- it('complex', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- regex: '(dolor|tempor)|inc[i]?d[^a]d.*nt',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
- });
- });
- });
- describe('boolean filtering', () => {
- it('eq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- active: {
- eq: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'E', 'F']);
- });
- it('isNull true', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableBoolean: {
- isNull: true,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
- });
- it('isNull false', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableBoolean: {
- isNull: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
- });
- });
- describe('number filtering', () => {
- it('eq', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- eq: 1,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B']);
- });
- it('lt', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- lt: 1,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A']);
- });
- it('lte', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- lte: 1,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
- });
- it('gt', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- gt: 1,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
- });
- it('gte', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- gte: 1,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E', 'F']);
- });
- it('between', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- order: {
- between: {
- start: 2,
- end: 4,
- },
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
- });
- it('isNull true', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableNumber: {
- isNull: true,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
- });
- it('isNull false', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableNumber: {
- isNull: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
- });
- });
- describe('date filtering', () => {
- it('before', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- date: {
- before: '2020-01-20T10:00:00.000Z',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
- });
- it('before on same date', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- date: {
- before: '2020-01-15T17:00:00.000Z',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
- });
- it('after', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- date: {
- after: '2020-01-20T10:00:00.000Z',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
- });
- it('after on same date', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- date: {
- after: '2020-01-25T09:00:00.000Z',
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
- });
- it('between', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- date: {
- between: {
- start: '2020-01-10T10:00:00.000Z',
- end: '2020-01-20T10:00:00.000Z',
- },
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B']);
- });
- it('isNull true', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableDate: {
- isNull: true,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'F']);
- });
- it('isNull false', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- nullableDate: {
- isNull: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E']);
- });
- });
- describe('multiple filters with filterOperator', () => {
- it('default AND', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- contains: 'Lorem',
- },
- active: {
- eq: false,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual([]);
- });
- it('explicit AND', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- contains: 'Lorem',
- },
- active: {
- eq: false,
- },
- },
- filterOperator: LogicalOperator.AND,
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual([]);
- });
- it('explicit OR', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- contains: 'Lorem',
- },
- active: {
- eq: false,
- },
- },
- filterOperator: LogicalOperator.OR,
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
- });
- it('explicit OR with 3 filters', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- description: {
- contains: 'eiusmod',
- },
- active: {
- eq: false,
- },
- order: {
- lt: 3,
- },
- },
- filterOperator: LogicalOperator.OR,
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
- });
- it('explicit OR with empty filters object', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {},
- filterOperator: LogicalOperator.OR,
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E', 'F']);
- });
- });
- describe('complex boolean logic with _and & _or', () => {
- it('single _and', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- _and: [
- {
- description: {
- contains: 'Lorem',
- },
- active: {
- eq: false,
- },
- },
- ],
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual([]);
- });
- it('single _or', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- _or: [
- {
- description: {
- contains: 'Lorem',
- },
- active: {
- eq: false,
- },
- },
- ],
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'E', 'F']);
- });
- it('_or with nested _and', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- _or: [
- {
- description: { contains: 'Lorem' },
- },
- {
- _and: [{ order: { gt: 3 } }, { order: { lt: 5 } }],
- },
- ],
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'E']);
- });
- it('_and with nested _or', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- _and: [
- {
- description: { contains: 'e' },
- },
- {
- _or: [{ active: { eq: false } }, { ownerId: { eq: '10' } }],
- },
- ],
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'F']);
- });
- });
- describe('sorting', () => {
- it('sort by string', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- label: SortOrder.DESC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
- });
- it('sort by number', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- order: SortOrder.DESC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
- });
- it('sort by date', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- date: SortOrder.DESC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
- });
- it('sort by ID', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- id: SortOrder.DESC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
- });
- it('sort by translated field en', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.name)).toEqual([
- 'apple',
- 'baum', // falling back to de here
- 'bike',
- 'cake',
- 'dog',
- 'egg',
- ]);
- });
- it('sort by translated field de', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- },
- },
- { languageCode: LanguageCode.de },
- );
- expect(testEntities.items.map((x: any) => x.name)).toEqual([
- 'apfel',
- 'baum',
- 'egg',
- 'fahrrad',
- 'hund',
- 'kuchen',
- ]);
- });
- it('sort by translated field en with take', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- take: 4,
- },
- });
- expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'baum', 'bike', 'cake']);
- });
- it('sort by translated field de with take', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- take: 4,
- },
- },
- { languageCode: LanguageCode.de },
- );
- expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'baum', 'egg', 'fahrrad']);
- });
- });
- describe('calculated fields', () => {
- it('filter by simple calculated property', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- descriptionLength: {
- lt: 12,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
- });
- it('filter by calculated property with join', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- filter: {
- price: {
- lt: 14,
- },
- },
- },
- });
- expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
- });
- it('sort by simple calculated property', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- descriptionLength: SortOrder.ASC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
- });
- it('sort by calculated property with join', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- price: SortOrder.ASC,
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
- });
- });
- describe('multiple clauses', () => {
- it('sort by translated field en & filter', async () => {
- const { testEntities } = await adminClient.query(GET_LIST, {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- filter: {
- order: {
- gte: 1,
- },
- },
- },
- });
- expect(testEntities.items.map((x: any) => x.name)).toEqual([
- 'baum',
- 'bike',
- 'cake',
- 'dog',
- 'egg',
- ]);
- });
- it('sort by translated field de & filter', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- filter: {
- order: {
- gte: 1,
- },
- },
- },
- },
- { languageCode: LanguageCode.de },
- );
- expect(testEntities.items.map((x: any) => x.name)).toEqual([
- 'baum',
- 'egg',
- 'fahrrad',
- 'hund',
- 'kuchen',
- ]);
- });
- it('sort by translated field de & filter & pagination', async () => {
- const { testEntities } = await adminClient.query(
- GET_LIST,
- {
- options: {
- sort: {
- name: SortOrder.ASC,
- },
- filter: {
- order: {
- gte: 1,
- },
- },
- take: 2,
- skip: 1,
- },
- },
- { languageCode: LanguageCode.de },
- );
- expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad']);
- });
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1586
- it('using the getMany() of the resulting QueryBuilder', async () => {
- const { testEntitiesGetMany } = await adminClient.query(GET_ARRAY_LIST, {});
- const actualPrices = testEntitiesGetMany.sort(sortById).map((x: any) => x.price).sort((a: number, b: number) => a - b);
- const expectedPrices = [11, 9, 22, 14, 13, 33].sort((a, b) => a - b);
- expect(actualPrices).toEqual(expectedPrices);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1611
- describe('translations handling', () => {
- const allTranslations = [
- [
- { languageCode: LanguageCode.en, name: 'apple' },
- { languageCode: LanguageCode.de, name: 'apfel' },
- ],
- [
- { languageCode: LanguageCode.en, name: 'bike' },
- { languageCode: LanguageCode.de, name: 'fahrrad' },
- ],
- ];
- function getTestEntityTranslations(testEntities: { items: any[] }) {
- // Explicitly sort the order of the translations as it was being non-deterministic on
- // the mysql CI tests.
- return testEntities.items.map((e: any) =>
- e.translations.sort((a: any, b: any) => (a.languageCode < b.languageCode ? 1 : -1)),
- );
- }
- it('returns all translations with default languageCode', async () => {
- const { testEntities } = await shopClient.query(GET_LIST_WITH_TRANSLATIONS, {
- options: {
- take: 2,
- sort: {
- order: SortOrder.ASC,
- },
- },
- });
- const testEntityTranslations = getTestEntityTranslations(testEntities);
- expect(testEntities.items.map((e: any) => e.name)).toEqual(['apple', 'bike']);
- expect(testEntityTranslations).toEqual(allTranslations);
- });
- it('returns all translations with non-default languageCode', async () => {
- const { testEntities } = await shopClient.query(
- GET_LIST_WITH_TRANSLATIONS,
- {
- options: {
- take: 2,
- sort: {
- order: SortOrder.ASC,
- },
- },
- },
- { languageCode: LanguageCode.de },
- );
- const testEntityTranslations = getTestEntityTranslations(testEntities);
- expect(testEntities.items.map((e: any) => e.name)).toEqual(['apfel', 'fahrrad']);
- expect(testEntityTranslations).toEqual(allTranslations);
- });
- });
- describe('customPropertyMap', () => {
- it('filter by custom string field', async () => {
- const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
- options: {
- sort: {
- label: SortOrder.ASC,
- },
- filter: {
- customerLastName: { contains: 'zieme' },
- },
- },
- });
- expect(testEntities.items).toEqual([
- {
- id: 'T_1',
- label: 'A',
- name: 'apple',
- parent: {
- id: 'T_2',
- label: 'B',
- name: 'bike',
- },
- orderRelation: {
- customer: {
- firstName: 'Hayden',
- lastName: 'Zieme',
- },
- id: 'T_1',
- },
- },
- {
- id: 'T_4',
- label: 'D',
- name: 'dog',
- parent: {
- id: 'T_2',
- label: 'B',
- name: 'bike',
- },
- orderRelation: {
- customer: {
- firstName: 'Hayden',
- lastName: 'Zieme',
- },
- id: 'T_4',
- },
- },
- ]);
- });
- it('sort by custom string field', async () => {
- const { testEntities } = await shopClient.query(GET_LIST_WITH_ORDERS, {
- options: {
- sort: {
- customerLastName: SortOrder.ASC,
- },
- },
- });
- expect(testEntities.items.map((i: any) => i.orderRelation.customer)).toEqual([
- { firstName: 'Trevor', lastName: 'Donnelly' },
- { firstName: 'Trevor', lastName: 'Donnelly' },
- { firstName: 'Marques', lastName: 'Sawayn' },
- { firstName: 'Marques', lastName: 'Sawayn' },
- { firstName: 'Hayden', lastName: 'Zieme' },
- { firstName: 'Hayden', lastName: 'Zieme' },
- ]);
- });
- });
- describe('relations in customFields', () => {
- it('should resolve relations in customFields successfully', async () => {
- const { testEntities } = await shopClient.query(GET_LIST_WITH_CUSTOM_FIELD_RELATION, {
- options: {
- filter: {
- label: { eq: 'A' },
- },
- },
- });
- expect(testEntities.items).toEqual([
- {
- id: 'T_1',
- label: 'A',
- customFields: {
- relation: [{ id: 'T_1', data: 'A' }],
- },
- },
- ]);
- });
- it('should resolve multiple relations in customFields successfully', async () => {
- const { testEntities } = await shopClient.query(GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION, {
- options: {
- filter: {
- label: { eq: 'A' },
- },
- },
- });
- expect(testEntities.items).toEqual([
- {
- id: 'T_1',
- label: 'A',
- customFields: {
- relation: [{ id: 'T_1', data: 'A' }],
- otherRelation: [{ id: 'T_1', data: 'A' }],
- },
- },
- ]);
- });
- });
- });
- const GET_LIST = gql`
- query GetTestEntities($options: TestEntityListOptions) {
- testEntities(options: $options) {
- totalItems
- items {
- id
- label
- name
- date
- }
- }
- }
- `;
- const GET_LIST_WITH_TRANSLATIONS = gql`
- query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
- testEntities(options: $options) {
- totalItems
- items {
- id
- label
- name
- date
- translations {
- languageCode
- name
- }
- }
- }
- }
- `;
- const GET_LIST_WITH_ORDERS = gql`
- query GetTestEntitiesWithTranslations($options: TestEntityListOptions) {
- testEntities(options: $options) {
- totalItems
- items {
- id
- label
- name
- parent {
- id
- label
- name
- }
- orderRelation {
- id
- customer {
- firstName
- lastName
- }
- }
- }
- }
- }
- `;
- const GET_ARRAY_LIST = gql`
- query GetTestEntitiesArray($options: TestEntityListOptions) {
- testEntitiesGetMany(options: $options) {
- id
- label
- name
- date
- price
- }
- }
- `;
- const GET_LIST_WITH_CUSTOM_FIELD_RELATION = gql`
- query GetTestWithCustomFieldRelation($options: TestEntityListOptions) {
- testEntities(options: $options) {
- items {
- id
- label
- customFields {
- relation {
- id
- data
- }
- }
- }
- }
- }
- `;
- const GET_LIST_WITH_MULTIPLE_CUSTOM_FIELD_RELATION = gql`
- query GetTestWithMultipleCustomFieldRelation($options: TestEntityListOptions) {
- testEntities(options: $options) {
- items {
- id
- label
- customFields {
- relation {
- id
- data
- }
- otherRelation {
- id
- data
- }
- }
- }
- }
- }
- `;
|