This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: finish e2e test of Announcement module
- Loading branch information
1 parent
0f5ab25
commit 2af1fd9
Showing
5 changed files
with
223 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { NestApplication } from '@nestjs/core' | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { MongooseModule } from '@nestjs/mongoose' | ||
import { GraphQLModule } from '@nestjs/graphql' | ||
import request from 'supertest' | ||
import { SCHEMA_GQL_FILE_NAME } from '../src/shared/constants' | ||
import { ConfigModule } from '../src/config/config.module' | ||
import { ConfigService } from '../src/config/config.service' | ||
import { AnnouncementsModule } from '../src/announcements/announcements.module' | ||
import { AnnouncementsModel } from '../src/announcements/models/announcements.model' | ||
import { CreateAnnouncementInput } from '../src/announcements/dtos/create-announcement.input' | ||
import { UpdateAnnouncementInput } from '../src/announcements/dtos/update-announcement.input' | ||
import { BatchDelete } from '../src/database/interfaces/batchDelete.interface' | ||
|
||
describe('AnnouncementsController (e2e)', () => { | ||
let app: NestApplication | ||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule, | ||
AnnouncementsModule, | ||
MongooseModule.forRootAsync({ | ||
useFactory: async (configService: ConfigService) => ({ | ||
uri: configService.getMongoURI(), | ||
useFindAndModify: false, | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true, | ||
useCreateIndex: true, | ||
}), | ||
inject: [ConfigService], | ||
}), | ||
GraphQLModule.forRoot({ | ||
autoSchemaFile: SCHEMA_GQL_FILE_NAME, | ||
}), | ||
], | ||
}).compile() | ||
app = moduleFixture.createNestApplication() | ||
await app.init() | ||
}) | ||
|
||
afterAll(async () => { | ||
await app.close() | ||
}) | ||
|
||
const createdData: CreateAnnouncementInput = { | ||
content: 'blog-be-next', | ||
} | ||
|
||
let id = '' | ||
|
||
const updatedData: UpdateAnnouncementInput = { | ||
id, | ||
content: 'blog-be-cms', | ||
} | ||
|
||
const createDataString = JSON.stringify(createdData).replace(/"([^(")"]+)":/g, '$1:') | ||
|
||
// CREATE_ONE | ||
it('createAnnouncement', async () => { | ||
const createOneTypeDefs = ` | ||
mutation CreateAnnouncement { | ||
createAnnouncement(input: ${createDataString}) { | ||
_id | ||
content | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: createOneTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: AnnouncementsModel = body.data.createAnnouncement | ||
id = testData._id | ||
expect(testData.content).toBe(createdData.content) | ||
}) | ||
.expect(200) | ||
}) | ||
|
||
// READ_ALL | ||
it('getAnnouncements', async () => { | ||
const getAllTypeDefs = ` | ||
query GetAnnouncements { | ||
getAnnouncements { | ||
_id | ||
content | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: getAllTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: AnnouncementsModel[] = body.data.getAnnouncements | ||
|
||
const firstData = testData[0] | ||
|
||
expect(testData.length).toBeGreaterThan(0) | ||
expect(firstData._id).toBe(id) | ||
expect(firstData.content).toBe(createdData.content) | ||
}) | ||
.expect(200) | ||
}) | ||
|
||
// READ_ONE | ||
it('getAnnouncementById', async () => { | ||
const getOneByIdTypeDefs = ` | ||
query GetAnnouncementById { | ||
getAnnouncementById(id: "${id}") { | ||
_id | ||
content | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: getOneByIdTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: AnnouncementsModel = body.data.getAnnouncementById | ||
|
||
expect(testData._id).toBe(id) | ||
expect(testData.content).toBe(createdData.content) | ||
}) | ||
.expect(200) | ||
}) | ||
|
||
// UPDATE_ONE | ||
it('updateAnnouncementById', async () => { | ||
const updateDataString = JSON.stringify({ ...updatedData, id }).replace(/"([^(")"]+)":/g, '$1:') | ||
|
||
const updateOneByIdTypeDefs = ` | ||
mutation UpdateAnnouncementById { | ||
updateAnnouncementById(input: ${updateDataString}) { | ||
_id | ||
content | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: updateOneByIdTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: AnnouncementsModel = body.data.updateAnnouncementById | ||
expect(testData.content).toBe(updatedData.content) | ||
}) | ||
.expect(200) | ||
}) | ||
|
||
// DELETE_ONE | ||
it('deleteAnnouncementById', async () => { | ||
const deleteOneByIdTypeDefs = ` | ||
mutation DeleteAnnouncementById { | ||
deleteAnnouncementById(id: "${id}") { | ||
_id | ||
content | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: deleteOneByIdTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: AnnouncementsModel = body.data.deleteAnnouncementById | ||
|
||
expect(testData.content).toBe(updatedData.content) | ||
}) | ||
.expect(200) | ||
}) | ||
|
||
// BATCH_DELETE | ||
it('deleteAnnouncements', async () => { | ||
const batchDeleteTypeDefs = ` | ||
mutation DeleteAnnouncements { | ||
deleteAnnouncements(ids: ["${id}"]) { | ||
ok | ||
n | ||
deletedCount | ||
} | ||
}` | ||
|
||
return request(app.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
query: batchDeleteTypeDefs, | ||
}) | ||
.expect(({ body }) => { | ||
const testData: BatchDelete = body.data.deleteAnnouncements | ||
expect(testData.ok).toBe(1) | ||
expect(testData.n).toBe(0) | ||
expect(testData.deletedCount).toBe(0) | ||
}) | ||
.expect(200) | ||
}) | ||
}) |