Browse Source

test(core): Add more e2e tests for ListQueryBuilder filters

Michael Bromley 5 years ago
parent
commit
bc26e7171b

+ 49 - 7
packages/core/e2e/fixtures/test-plugins/list-query-plugin.ts

@@ -18,6 +18,15 @@ export class TestEntity extends VendureEntity {
     @Column()
     label: string;
 
+    @Column()
+    description: string;
+
+    @Column()
+    active: boolean;
+
+    @Column()
+    order: number;
+
     @Column()
     date: Date;
 }
@@ -46,6 +55,9 @@ const adminApiExtensions = gql`
         createdAt: DateTime!
         updatedAt: DateTime!
         label: String!
+        description: String!
+        active: Boolean!
+        order: Int!
         date: DateTime!
     }
 
@@ -75,13 +87,43 @@ export class ListQueryPlugin implements OnVendureBootstrap {
     async onVendureBootstrap() {
         const count = await this.connection.getRepository(TestEntity).count();
         if (count === 0) {
-            await this.connection
-                .getRepository(TestEntity)
-                .save([
-                    new TestEntity({ label: 'A', date: new Date('2020-01-05T10:00:00.000Z') }),
-                    new TestEntity({ label: 'B', date: new Date('2020-01-15T10:00:00.000Z') }),
-                    new TestEntity({ label: 'C', date: new Date('2020-01-25T10:00:00.000Z') }),
-                ]);
+            await this.connection.getRepository(TestEntity).save([
+                new TestEntity({
+                    label: 'A',
+                    description: 'Lorem ipsum',
+                    date: new Date('2020-01-05T10:00:00.000Z'),
+                    active: true,
+                    order: 0,
+                }),
+                new TestEntity({
+                    label: 'B',
+                    description: 'dolor sit',
+                    date: new Date('2020-01-15T10:00:00.000Z'),
+                    active: true,
+                    order: 1,
+                }),
+                new TestEntity({
+                    label: 'C',
+                    description: 'consectetur adipiscing',
+                    date: new Date('2020-01-25T10:00:00.000Z'),
+                    active: false,
+                    order: 2,
+                }),
+                new TestEntity({
+                    label: 'D',
+                    description: 'eiusmod tempor',
+                    date: new Date('2020-01-30T10:00:00.000Z'),
+                    active: true,
+                    order: 3,
+                }),
+                new TestEntity({
+                    label: 'E',
+                    description: 'incididunt ut',
+                    date: new Date('2020-02-05T10:00:00.000Z'),
+                    active: false,
+                    order: 4,
+                }),
+            ]);
         }
     }
 }

+ 138 - 3
packages/core/e2e/list-query-builder.e2e-spec.ts

@@ -4,7 +4,7 @@ import gql from 'graphql-tag';
 import path from 'path';
 
 import { initialData } from '../../../e2e-common/e2e-initial-data';
-import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
 
 import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
 import { fixPostgresTimezone } from './utils/fix-pg-timezone';
@@ -35,6 +35,141 @@ describe('ListQueryBuilder', () => {
         return items.map((x: any) => x.label).sort();
     }
 
+    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('contains', async () => {
+            const { testEntities } = await adminClient.query(GET_LIST, {
+                options: {
+                    filter: {
+                        description: {
+                            contains: 'adip',
+                        },
+                    },
+                },
+            });
+
+            expect(getItemLabels(testEntities.items)).toEqual(['C']);
+        });
+    });
+
+    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']);
+        });
+    });
+
+    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']);
+        });
+
+        it('gte', async () => {
+            const { testEntities } = await adminClient.query(GET_LIST, {
+                options: {
+                    filter: {
+                        order: {
+                            gte: 1,
+                        },
+                    },
+                },
+            });
+
+            expect(getItemLabels(testEntities.items)).toEqual(['B', 'C', 'D', 'E']);
+        });
+
+        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']);
+        });
+    });
+
     describe('date filtering', () => {
         it('before', async () => {
             const { testEntities } = await adminClient.query(GET_LIST, {
@@ -75,7 +210,7 @@ describe('ListQueryBuilder', () => {
                 },
             });
 
-            expect(getItemLabels(testEntities.items)).toEqual(['C']);
+            expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
         });
 
         it('after on same date', async () => {
@@ -89,7 +224,7 @@ describe('ListQueryBuilder', () => {
                 },
             });
 
-            expect(getItemLabels(testEntities.items)).toEqual(['C']);
+            expect(getItemLabels(testEntities.items)).toEqual(['C', 'D', 'E']);
         });
 
         it('between', async () => {