Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
feat: finish PlayerController e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Dec 27, 2019
1 parent 964933f commit b30570c
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 26 deletions.
49 changes: 23 additions & 26 deletions schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,39 @@ type BatchDeleteModel {
deletedCount: Float
}

type BestAlbumModel {
_id: ID!
input CreateOpenSourceInput {
title: String!
artist: String!
coverUrl: String!
mvUrl: String!
releaseDate: String!
createdAt: String!
updatedAt: String!
description: String!
url: String!
posterUrl: String!
}

input CreateBestAlbumInput {
title: String!
artist: String!
coverUrl: String!
mvUrl: String!
releaseDate: String!
type Mutation {
createOpenSource(input: CreateOpenSourceInput!): OpenSourceModel!
updateOpenSourceById(input: UpdateOpenSourceInput!): OpenSourceModel!
deleteOpenSourceById(id: ID!): OpenSourceModel!
deleteOpenSources(ids: [ID!]!): BatchDeleteModel!
}

type Mutation {
createBestAlbum(input: CreateBestAlbumInput!): BestAlbumModel!
updateBestAlbumById(input: UpdateBestAlbumInput!): BestAlbumModel!
deleteBestAlbumById(id: ID!): BestAlbumModel!
deleteBestAlbums(ids: [ID!]!): BatchDeleteModel!
type OpenSourceModel {
_id: ID!
title: String!
description: String!
url: String!
posterUrl: String!
createdAt: String!
updatedAt: String!
}

type Query {
getBestAlbums: [BestAlbumModel!]!
getBestAlbumById(id: ID!): BestAlbumModel!
getOpenSources: [OpenSourceModel!]!
getOpenSourceById(id: ID!): OpenSourceModel!
}

input UpdateBestAlbumInput {
input UpdateOpenSourceInput {
title: String!
artist: String!
coverUrl: String!
mvUrl: String!
releaseDate: String!
description: String!
url: String!
posterUrl: String!
id: String!
}
271 changes: 271 additions & 0 deletions test/player.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
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 { PlayerModule } from '../src/player/player.module'
import { PlayerModel } from '../src/player/models/player.model'
import { CreatePlayerInput } from '../src/player/dtos/create-player.input'
import { UpdatePlayerInput } from '../src/player/dtos/update-player.input'
import { BatchDelete } from '../src/database/interfaces/batchDelete.interface'

describe('PlayerController (e2e)', () => {
let app: NestApplication
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule,
PlayerModule,
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: CreatePlayerInput = {
title: 'イチリンソウ',
artist: '山本彩',
lrc: '歌词',
coverUrl: 'https://t4est3.com',
musicFileUrl: 'https://1test4.com',
}

let id = ''

const updatedData: UpdatePlayerInput = {
id,
title: '夕焼けの歌',
artist: '近藤真彦',
lrc: '歌词',
coverUrl: 'https://t5est4.com',
musicFileUrl: 'https://2test5.com',
}

const createDataString = JSON.stringify(createdData).replace(/"([^(")"]+)":/g, '$1:')

// CREATE_ONE
it('createPlayer', () => {
const createOneTypeDefs = `
mutation CreatePlayer {
createPlayer(input: ${createDataString}) {
_id
title
artist
lrc
coverUrl
musicFileUrl
createdAt
updatedAt
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: createOneTypeDefs,
})
.expect(({ body }) => {
const testData: PlayerModel = body.data.createPlayer

id = testData._id

expect(testData.title).toBe(createdData.title)
expect(testData.artist).toBe(createdData.artist)
expect(testData.lrc).toBe(createdData.lrc)
expect(testData.coverUrl).toBe(createdData.coverUrl)
expect(testData.musicFileUrl).toBe(createdData.musicFileUrl)
})
.expect(200)
})

// READ_ALL
it('getPlayer', () => {
const getAllTypeDefs = `
query GetPlayer {
getPlayer {
_id
title
artist
lrc
coverUrl
musicFileUrl
createdAt
updatedAt
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: getAllTypeDefs,
})
.expect(({ body }) => {
const testData: PlayerModel[] = body.data.getPlayer

const firstData = testData[0]

expect(testData.length).toBeGreaterThan(0)
expect(firstData._id).toBe(id)
expect(firstData.title).toBe(createdData.title)
expect(firstData.title).toBe(createdData.title)
expect(firstData.artist).toBe(createdData.artist)
expect(firstData.lrc).toBe(createdData.lrc)
expect(firstData.coverUrl).toBe(createdData.coverUrl)
expect(firstData.musicFileUrl).toBe(createdData.musicFileUrl)
})
.expect(200)
})

// READ_ONE
it('getPlayerById', () => {
const getOneByIdTypeDefs = `
query GetPlayerById {
getPlayerById(id: "${id}") {
_id
title
artist
lrc
coverUrl
musicFileUrl
createdAt
updatedAt
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: getOneByIdTypeDefs,
})
.expect(({ body }) => {
const testData: PlayerModel = body.data.getPlayerById

expect(testData._id).toBe(id)
expect(testData.title).toBe(createdData.title)
expect(testData.artist).toBe(createdData.artist)
expect(testData.lrc).toBe(createdData.lrc)
expect(testData.coverUrl).toBe(createdData.coverUrl)
expect(testData.musicFileUrl).toBe(createdData.musicFileUrl)
})
.expect(200)
})

// UPDATE_ONE
it('updatePlayerById', () => {
const updateDataString = JSON.stringify({ ...updatedData, id }).replace(/"([^(")"]+)":/g, '$1:')

const updateOneByIdTypeDefs = `
mutation UpdatePlayerById {
updatePlayerById(input: ${updateDataString}) {
_id
title
artist
lrc
coverUrl
musicFileUrl
createdAt
updatedAt
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: updateOneByIdTypeDefs,
})
.expect(({ body }) => {
const testData: PlayerModel = body.data.updatePlayerById

expect(testData.title).toBe(updatedData.title)
expect(testData.artist).toBe(updatedData.artist)
expect(testData.lrc).toBe(updatedData.lrc)
expect(testData.coverUrl).toBe(updatedData.coverUrl)
expect(testData.musicFileUrl).toBe(updatedData.musicFileUrl)
})
.expect(200)
})

// DELETE_ONE
it('deletePlayerById', () => {
const deleteOneByIdTypeDefs = `
mutation DeletePlayerById {
deletePlayerById(id: "${id}") {
_id
title
artist
lrc
coverUrl
musicFileUrl
createdAt
updatedAt
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: deleteOneByIdTypeDefs,
})
.expect(({ body }) => {
const testData: PlayerModel = body.data.deletePlayerById

expect(testData.title).toBe(updatedData.title)
expect(testData.artist).toBe(updatedData.artist)
expect(testData.lrc).toBe(updatedData.lrc)
expect(testData.coverUrl).toBe(updatedData.coverUrl)
expect(testData.musicFileUrl).toBe(updatedData.musicFileUrl)
})
.expect(200)
})

// BATCH_DELETE
it('deletePlayer', () => {
const batchDeleteTypeDefs = `
mutation DeletePlayer {
deletePlayer(ids: ["${id}"]) {
ok
n
deletedCount
}
}`

return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
query: batchDeleteTypeDefs,
})
.expect(({ body }) => {
const testData: BatchDelete = body.data.deletePlayer
expect(testData.ok).toBe(1)
expect(testData.n).toBe(0)
expect(testData.deletedCount).toBe(0)
})
.expect(200)
})
})

0 comments on commit b30570c

Please sign in to comment.