Skip to content

Commit a954f81

Browse files
test(nestjs-graphql): Add tests on enableFetchAllWithNegative option
1 parent 6bfc92f commit a954f81

17 files changed

+508
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Connection } from 'typeorm'
2+
3+
import { executeTruncate } from '../../helpers'
4+
import { TodoItemEntity } from '../src/todo-item/todo-item.entity'
5+
6+
const tables = ['todo_item']
7+
export const truncate = async (connection: Connection): Promise<void> => executeTruncate(connection, tables)
8+
9+
export let todoItems: TodoItemEntity[]
10+
11+
export const refresh = async (connection: Connection): Promise<void> => {
12+
await truncate(connection)
13+
14+
const todoRepo = connection.getRepository(TodoItemEntity)
15+
16+
todoItems = await todoRepo.save(
17+
Array.from({ length: 100 }, (_, index) => ({
18+
title: `todoTitle${index + 1}`,
19+
completed: index % 2 === 0
20+
}))
21+
)
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export const todoItemFields = `
2+
id
3+
title
4+
completed
5+
`
6+
7+
export const cursorPageInfoField = `
8+
pageInfo{
9+
hasNextPage
10+
hasPreviousPage
11+
startCursor
12+
endCursor
13+
}
14+
`
15+
16+
export const offsetPageInfoField = `
17+
pageInfo{
18+
hasNextPage
19+
hasPreviousPage
20+
}
21+
`
22+
23+
export const nodes = (fields: string): string => `
24+
nodes{
25+
${fields}
26+
}
27+
`
28+
29+
export const edgeNodes = (fields: string): string => `
30+
edges {
31+
node{
32+
${fields}
33+
}
34+
cursor
35+
}
36+
`
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { INestApplication, ValidationPipe } from '@nestjs/common'
2+
import { Test } from '@nestjs/testing'
3+
import { CursorConnectionType } from '@ptc-org/nestjs-query-graphql'
4+
import request from 'supertest'
5+
import { Connection } from 'typeorm'
6+
7+
import { AppModule } from '../src/app.module'
8+
import { TodoItemCursorFetchWithNegativeDisableDTO } from '../src/todo-item/dto/todo-item-cursor-fetch-all-disable.dto'
9+
import { refresh } from './fixtures'
10+
import { cursorPageInfoField, edgeNodes, todoItemFields } from './graphql-fragments'
11+
12+
describe('TodoItemResolver (cursor paginatino - fetch all with negative disabled)', () => {
13+
let app: INestApplication
14+
15+
beforeAll(async () => {
16+
const moduleRef = await Test.createTestingModule({
17+
imports: [AppModule]
18+
}).compile()
19+
20+
app = moduleRef.createNestApplication()
21+
app.useGlobalPipes(
22+
new ValidationPipe({
23+
transform: true,
24+
whitelist: true,
25+
forbidNonWhitelisted: true,
26+
skipMissingProperties: false,
27+
forbidUnknownValues: true
28+
})
29+
)
30+
31+
await app.init()
32+
await refresh(app.get(Connection))
33+
})
34+
35+
afterAll(() => refresh(app.get(Connection)))
36+
37+
describe('query', () => {
38+
describe('paging', () => {
39+
it('should not allow to fetch all the nodes', () =>
40+
request(app.getHttpServer())
41+
.post('/graphql')
42+
.send({
43+
operationName: null,
44+
variables: {},
45+
query: `{
46+
todoItemCursorFetchWithNegativeDisables(paging: {first: -1, after: "YXJyYXljb25uZWN0aW9uOjE="}) {
47+
${cursorPageInfoField}
48+
${edgeNodes(todoItemFields)}
49+
}
50+
}`
51+
})
52+
.expect(200)
53+
.then(({ body }) => {
54+
const { edges, pageInfo }: CursorConnectionType<TodoItemCursorFetchWithNegativeDisableDTO> =
55+
body.data.todoItemCursorFetchWithNegativeDisables
56+
expect(pageInfo).toEqual({
57+
endCursor: null,
58+
hasNextPage: false,
59+
hasPreviousPage: false,
60+
startCursor: null
61+
})
62+
expect(edges).toHaveLength(0)
63+
}))
64+
})
65+
})
66+
67+
afterAll(async () => {
68+
await app.close()
69+
})
70+
})
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { INestApplication, ValidationPipe } from '@nestjs/common'
2+
import { Test } from '@nestjs/testing'
3+
import { CursorConnectionType } from '@ptc-org/nestjs-query-graphql'
4+
import request from 'supertest'
5+
import { Connection } from 'typeorm'
6+
7+
import { AppModule } from '../src/app.module'
8+
import { TodoItemCursorFetchWithNegativeEnableDTO } from '../src/todo-item/dto/todo-item-cursor-fetch-all-enable.dto'
9+
import { refresh, todoItems } from './fixtures'
10+
import { cursorPageInfoField, edgeNodes, todoItemFields } from './graphql-fragments'
11+
12+
describe('TodoItemResolver (cursor paginatino - fetch all with negative enabled)', () => {
13+
let app: INestApplication
14+
15+
beforeAll(async () => {
16+
const moduleRef = await Test.createTestingModule({
17+
imports: [AppModule]
18+
}).compile()
19+
20+
app = moduleRef.createNestApplication()
21+
app.useGlobalPipes(
22+
new ValidationPipe({
23+
transform: true,
24+
whitelist: true,
25+
forbidNonWhitelisted: true,
26+
skipMissingProperties: false,
27+
forbidUnknownValues: true
28+
})
29+
)
30+
31+
await app.init()
32+
await refresh(app.get(Connection))
33+
})
34+
35+
afterAll(() => refresh(app.get(Connection)))
36+
37+
describe('query', () => {
38+
describe('paging', () => {
39+
it('should return all the nodes after the given cursor', () =>
40+
request(app.getHttpServer())
41+
.post('/graphql')
42+
.send({
43+
operationName: null,
44+
variables: {},
45+
query: `{
46+
todoItemCursorFetchWithNegativeEnables(paging: {first: -1, after: "YXJyYXljb25uZWN0aW9uOjE="}) {
47+
${cursorPageInfoField}
48+
${edgeNodes(todoItemFields)}
49+
}
50+
}`
51+
})
52+
.expect(200)
53+
.then(({ body }) => {
54+
const { edges, pageInfo }: CursorConnectionType<TodoItemCursorFetchWithNegativeEnableDTO> =
55+
body.data.todoItemCursorFetchWithNegativeEnables
56+
expect(pageInfo).toEqual({
57+
endCursor: 'YXJyYXljb25uZWN0aW9uOjk5',
58+
hasNextPage: false,
59+
hasPreviousPage: true,
60+
startCursor: 'YXJyYXljb25uZWN0aW9uOjI='
61+
})
62+
expect(edges).toHaveLength(98)
63+
64+
expect(edges.map((e) => e.node)).toEqual(todoItems.slice(2))
65+
}))
66+
it('should return all the nodes before the given cursor', () =>
67+
request(app.getHttpServer())
68+
.post('/graphql')
69+
.send({
70+
operationName: null,
71+
variables: {},
72+
query: `{
73+
todoItemCursorFetchWithNegativeEnables(paging: {last: -1, before: "YXJyYXljb25uZWN0aW9uOjk4"}) {
74+
${cursorPageInfoField}
75+
${edgeNodes(todoItemFields)}
76+
}
77+
}`
78+
})
79+
.expect(200)
80+
.then(({ body }) => {
81+
const { edges, pageInfo }: CursorConnectionType<TodoItemCursorFetchWithNegativeEnableDTO> =
82+
body.data.todoItemCursorFetchWithNegativeEnables
83+
expect(pageInfo).toEqual({
84+
endCursor: 'YXJyYXljb25uZWN0aW9uOjk3',
85+
hasNextPage: true,
86+
hasPreviousPage: false,
87+
startCursor: 'YXJyYXljb25uZWN0aW9uOjA='
88+
})
89+
expect(edges).toHaveLength(98)
90+
91+
expect(edges.map((e) => e.node)).toEqual(todoItems.slice(0, 98))
92+
}))
93+
})
94+
})
95+
96+
afterAll(async () => {
97+
await app.close()
98+
})
99+
})
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { INestApplication, ValidationPipe } from '@nestjs/common'
2+
import { Test } from '@nestjs/testing'
3+
import { OffsetConnectionType } from '@ptc-org/nestjs-query-graphql'
4+
import request from 'supertest'
5+
import { Connection } from 'typeorm'
6+
7+
import { AppModule } from '../src/app.module'
8+
import { TodoItemOffsetFetchWithNegativeDisableDTO } from '../src/todo-item/dto/todo-item-offset-fetch-all-disable.dto'
9+
import { refresh } from './fixtures'
10+
import { nodes as graphqlNodes, offsetPageInfoField, todoItemFields } from './graphql-fragments'
11+
12+
describe('TodoItemResolver (offset pagination - fetch all with negative disabled)', () => {
13+
let app: INestApplication
14+
15+
beforeAll(async () => {
16+
const moduleRef = await Test.createTestingModule({
17+
imports: [AppModule]
18+
}).compile()
19+
20+
app = moduleRef.createNestApplication()
21+
app.useGlobalPipes(
22+
new ValidationPipe({
23+
transform: true,
24+
whitelist: true,
25+
forbidNonWhitelisted: true,
26+
skipMissingProperties: false,
27+
forbidUnknownValues: true
28+
})
29+
)
30+
31+
await app.init()
32+
await refresh(app.get(Connection))
33+
})
34+
35+
afterAll(() => refresh(app.get(Connection)))
36+
37+
describe('query', () => {
38+
describe('paging', () => {
39+
it('should not allow to fetch all the nodes', () =>
40+
request(app.getHttpServer())
41+
.post('/graphql')
42+
.send({
43+
operationName: null,
44+
variables: {},
45+
query: `{
46+
todoItemOffsetFetchWithNegativeDisables(paging: {limit: -1, offset: 2}) {
47+
${offsetPageInfoField}
48+
${graphqlNodes(todoItemFields)}
49+
}
50+
}`
51+
})
52+
.expect(200)
53+
.then(({ body }) => {
54+
const { nodes, pageInfo }: OffsetConnectionType<TodoItemOffsetFetchWithNegativeDisableDTO> =
55+
body.data.todoItemOffsetFetchWithNegativeDisables
56+
expect(pageInfo).toEqual({
57+
hasNextPage: false,
58+
hasPreviousPage: false
59+
})
60+
expect(nodes).toHaveLength(0)
61+
}))
62+
})
63+
})
64+
65+
afterAll(async () => {
66+
await app.close()
67+
})
68+
})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { INestApplication, ValidationPipe } from '@nestjs/common'
2+
import { Test } from '@nestjs/testing'
3+
import { OffsetConnectionType } from '@ptc-org/nestjs-query-graphql'
4+
import request from 'supertest'
5+
import { Connection } from 'typeorm'
6+
7+
import { AppModule } from '../src/app.module'
8+
import { TodoItemOffsetFetchWithNegativeEnableDTO } from '../src/todo-item/dto/todo-item-offset-fetch-all-enable.dto'
9+
import { refresh, todoItems } from './fixtures'
10+
import { nodes as graphqlNodes, offsetPageInfoField, todoItemFields } from './graphql-fragments'
11+
12+
describe('TodoItemResolver (offset pagination - fetch all with negative enabled)', () => {
13+
let app: INestApplication
14+
15+
beforeAll(async () => {
16+
const moduleRef = await Test.createTestingModule({
17+
imports: [AppModule]
18+
}).compile()
19+
20+
app = moduleRef.createNestApplication()
21+
app.useGlobalPipes(
22+
new ValidationPipe({
23+
transform: true,
24+
whitelist: true,
25+
forbidNonWhitelisted: true,
26+
skipMissingProperties: false,
27+
forbidUnknownValues: true
28+
})
29+
)
30+
31+
await app.init()
32+
await refresh(app.get(Connection))
33+
})
34+
35+
afterAll(() => refresh(app.get(Connection)))
36+
37+
describe('query', () => {
38+
describe('paging', () => {
39+
it('should return all the nodes after the given offset', () =>
40+
request(app.getHttpServer())
41+
.post('/graphql')
42+
.send({
43+
operationName: null,
44+
variables: {},
45+
query: `{
46+
todoItemOffsetFetchWithNegativeEnables(paging: {limit: -1, offset: 2}) {
47+
${offsetPageInfoField}
48+
${graphqlNodes(todoItemFields)}
49+
}
50+
}`
51+
})
52+
.expect(200)
53+
.then(({ body }) => {
54+
const { nodes, pageInfo }: OffsetConnectionType<TodoItemOffsetFetchWithNegativeEnableDTO> =
55+
body.data.todoItemOffsetFetchWithNegativeEnables
56+
expect(pageInfo).toEqual({
57+
hasNextPage: false,
58+
hasPreviousPage: true
59+
})
60+
expect(nodes).toHaveLength(98)
61+
62+
expect(nodes).toEqual(todoItems.slice(2))
63+
}))
64+
})
65+
})
66+
67+
afterAll(async () => {
68+
await app.close()
69+
})
70+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ApolloDriver } from '@nestjs/apollo'
2+
import { Module } from '@nestjs/common'
3+
import { GraphQLModule } from '@nestjs/graphql'
4+
import { TypeOrmModule } from '@nestjs/typeorm'
5+
6+
import { formatGraphqlError, typeormOrmConfig } from '../../helpers'
7+
import { TodoItemModule } from './todo-item/todo-item.module'
8+
9+
@Module({
10+
imports: [
11+
TypeOrmModule.forRoot(typeormOrmConfig('fetch_all_with_negative')),
12+
GraphQLModule.forRoot({
13+
driver: ApolloDriver,
14+
autoSchemaFile: 'schema.gql',
15+
formatError: formatGraphqlError
16+
}),
17+
TodoItemModule
18+
]
19+
})
20+
export class AppModule {}

0 commit comments

Comments
 (0)