Skip to content

Commit

Permalink
fix(core): Correct handling of non-default languages in ListQueryBuilder
Browse files Browse the repository at this point in the history
Fixes #980
  • Loading branch information
michaelbromley committed Jul 16, 2021
1 parent 511c2ed commit 837840e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 38 deletions.
8 changes: 8 additions & 0 deletions packages/core/e2e/fixtures/test-plugins/list-query-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ export class ListQueryPlugin implements OnApplicationBootstrap {
active: false,
order: 4,
}),
new TestEntity({
label: 'F',
description: 'quis nostrud exercitation ullamco', // 33
date: new Date('2020-02-07T10:00:00.000Z'),
active: false,
order: 5,
}),
]);

const translations: any = {
Expand All @@ -222,6 +229,7 @@ export class ListQueryPlugin implements OnApplicationBootstrap {
C: { [LanguageCode.en]: 'cake', [LanguageCode.de]: 'kuchen' },
D: { [LanguageCode.en]: 'dog', [LanguageCode.de]: 'hund' },
E: { [LanguageCode.en]: 'egg' },
F: { [LanguageCode.de]: 'baum' },
};

for (const testEntity of testEntities) {
Expand Down
84 changes: 50 additions & 34 deletions packages/core/e2e/list-query-builder.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ describe('ListQueryBuilder', () => {
{ languageCode: LanguageCode.en },
);

expect(testEntities.totalItems).toBe(5);
expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
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([
'apple',
'bike',
'cake',
'dog',
'egg',
'baum', // if default en lang does not exist, use next available lang
]);
});

Expand All @@ -71,14 +72,15 @@ describe('ListQueryBuilder', () => {
{ languageCode: LanguageCode.de },
);

expect(testEntities.totalItems).toBe(5);
expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'C', 'D', 'E']);
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([
'apfel',
'fahrrad',
'kuchen',
'hund',
'egg', // falls back to en translation when de doesn't exist
'baum',
]);
});

Expand All @@ -89,7 +91,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.totalItems).toBe(5);
expect(testEntities.totalItems).toBe(6);
expect(getItemLabels(testEntities.items)).toEqual(['A', 'B']);
});

Expand All @@ -100,8 +102,8 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.totalItems).toBe(5);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
expect(testEntities.totalItems).toBe(6);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
});

it('skip negative is ignored', async () => {
Expand All @@ -111,8 +113,8 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.totalItems).toBe(5);
expect(testEntities.items.length).toBe(5);
expect(testEntities.totalItems).toBe(6);
expect(testEntities.items.length).toBe(6);
});

it('take zero is ignored', async () => {
Expand All @@ -122,8 +124,8 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.totalItems).toBe(5);
expect(testEntities.items.length).toBe(5);
expect(testEntities.totalItems).toBe(6);
expect(testEntities.items.length).toBe(6);
});

it('take negative is ignored', async () => {
Expand All @@ -133,8 +135,8 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.totalItems).toBe(5);
expect(testEntities.items.length).toBe(5);
expect(testEntities.totalItems).toBe(6);
expect(testEntities.items.length).toBe(6);
});

it(
Expand Down Expand Up @@ -186,7 +188,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['A', 'C', 'D', 'E', 'F']);
});

it('contains', async () => {
Expand Down Expand Up @@ -214,7 +216,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['A', 'B', 'E', 'F']);
});

it('in', async () => {
Expand Down Expand Up @@ -242,7 +244,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['B', 'D', 'E', 'F']);
});

describe('regex', () => {
Expand Down Expand Up @@ -330,7 +332,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['C', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'E', 'F']);
});
});

Expand Down Expand Up @@ -388,7 +390,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
});

it('gte', async () => {
Expand All @@ -402,7 +404,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E', 'F']);
});

it('between', async () => {
Expand Down Expand Up @@ -463,7 +465,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
});

it('after on same date', async () => {
Expand All @@ -477,7 +479,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E', 'F']);
});

it('between', async () => {
Expand Down Expand Up @@ -508,7 +510,7 @@ describe('ListQueryBuilder', () => {
},
});

expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
});

it('sort by number', async () => {
Expand All @@ -519,7 +521,7 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
});

it('sort by date', async () => {
Expand All @@ -530,7 +532,7 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
});

it('sort by ID', async () => {
Expand All @@ -541,7 +543,7 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.label)).toEqual(['E', 'D', 'C', 'B', 'A']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['F', 'E', 'D', 'C', 'B', 'A']);
});

it('sort by translated field en', async () => {
Expand All @@ -554,6 +556,7 @@ describe('ListQueryBuilder', () => {
});
expect(testEntities.items.map((x: any) => x.name)).toEqual([
'apple',
'baum', // falling back to de here
'bike',
'cake',
'dog',
Expand All @@ -575,6 +578,7 @@ describe('ListQueryBuilder', () => {
);
expect(testEntities.items.map((x: any) => x.name)).toEqual([
'apfel',
'baum',
'egg',
'fahrrad',
'hund',
Expand All @@ -588,10 +592,10 @@ describe('ListQueryBuilder', () => {
sort: {
name: SortOrder.ASC,
},
take: 3,
take: 4,
},
});
expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'bike', 'cake']);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['apple', 'baum', 'bike', 'cake']);
});

it('sort by translated field de with take', async () => {
Expand All @@ -602,12 +606,12 @@ describe('ListQueryBuilder', () => {
sort: {
name: SortOrder.ASC,
},
take: 3,
take: 4,
},
},
{ languageCode: LanguageCode.de },
);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'egg', 'fahrrad']);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['apfel', 'baum', 'egg', 'fahrrad']);
});
});

Expand Down Expand Up @@ -646,7 +650,7 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
});

it('sort by calculated property with join', async () => {
Expand All @@ -657,7 +661,7 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C']);
expect(testEntities.items.map((x: any) => x.label)).toEqual(['B', 'A', 'E', 'D', 'C', 'F']);
});
});

Expand All @@ -675,7 +679,13 @@ describe('ListQueryBuilder', () => {
},
},
});
expect(testEntities.items.map((x: any) => x.name)).toEqual(['bike', 'cake', 'dog', 'egg']);
expect(testEntities.items.map((x: any) => x.name)).toEqual([
'baum',
'bike',
'cake',
'dog',
'egg',
]);
});

it('sort by translated field de & filter', async () => {
Expand All @@ -695,7 +705,13 @@ describe('ListQueryBuilder', () => {
},
{ languageCode: LanguageCode.de },
);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad', 'hund', 'kuchen']);
expect(testEntities.items.map((x: any) => x.name)).toEqual([
'baum',
'egg',
'fahrrad',
'hund',
'kuchen',
]);
});

it('sort by translated field de & filter & pagination', async () => {
Expand All @@ -717,7 +733,7 @@ describe('ListQueryBuilder', () => {
},
{ languageCode: LanguageCode.de },
);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['fahrrad', 'hund']);
expect(testEntities.items.map((x: any) => x.name)).toEqual(['egg', 'fahrrad']);
});
});
});
Expand Down
49 changes: 49 additions & 0 deletions packages/core/e2e/product.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,55 @@ describe('Product resolver', () => {
deletedVariant.options.map(o => o.code).sort(),
);
});

// https://github.com/vendure-ecommerce/vendure/issues/980
it('creating variants in a non-default language', async () => {
const { createProduct } = await adminClient.query<
CreateProduct.Mutation,
CreateProduct.Variables
>(CREATE_PRODUCT, {
input: {
translations: [
{
languageCode: LanguageCode.de,
name: 'Ananas',
slug: 'ananas',
description: 'Yummy Ananas',
},
],
},
});

const { createProductVariants } = await adminClient.query<
CreateProductVariants.Mutation,
CreateProductVariants.Variables
>(CREATE_PRODUCT_VARIANTS, {
input: [
{
productId: createProduct.id,
sku: 'AN1110111',
optionIds: [],
translations: [{ languageCode: LanguageCode.de, name: 'Ananas Klein' }],
},
],
});

expect(createProductVariants.length).toBe(1);
expect(createProductVariants[0]?.name).toBe('Ananas Klein');

const { product } = await adminClient.query<
GetProductWithVariants.Query,
GetProductWithVariants.Variables
>(
GET_PRODUCT_WITH_VARIANTS,
{
id: createProduct.id,
},
{ languageCode: LanguageCode.en },
);

expect(product?.variants.length).toBe(1);
});
});
});

Expand Down
Loading

0 comments on commit 837840e

Please sign in to comment.