Created
March 25, 2020 16:08
-
-
Save nietzscheson/c106fbe9f4b2dadd4e92bdd563a60f5c to your computer and use it in GitHub Desktop.
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
import { Test, TestingModule } from '@nestjs/testing'; | |
import { INestApplication } from '@nestjs/common'; | |
import * as request from 'supertest'; | |
import { AppModule } from '../src/app.module'; | |
import { User } from 'src/user/user.entity'; | |
import { Connection, Repository } from 'typeorm'; | |
import * as faker from 'faker'; | |
describe('User Resolver', () => { | |
let app: INestApplication; | |
let id: string = ''; | |
let repository: Repository<User>; | |
let connection: Connection; | |
const createUser = { | |
firstName: faker.name.firstName(), | |
lastName: faker.name.lastName(), | |
email: faker.internet.email(), | |
password: faker.internet.password(), | |
phoneNumber: faker.phone.phoneNumber(), | |
birthday: "11/02/1969", | |
gendre: 'F' | |
}; | |
const updateUser = { | |
firstName: faker.name.firstName(), | |
lastName: faker.name.lastName(), | |
email: faker.internet.email(), | |
password: faker.internet.password(), | |
phoneNumber: faker.phone.phoneNumber(), | |
birthday: "04/07/1975", | |
gendre: 'F' | |
}; | |
beforeAll(async () => { | |
const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule] }).compile(); | |
repository = moduleFixture.get('UserRepository'); | |
connection = moduleFixture.get(Connection); | |
app = await moduleFixture.createNestApplication().init(); | |
}); | |
afterAll(async () => { | |
await connection.dropDatabase(); | |
await app.close(); | |
}); | |
it('Create User', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `mutation { | |
createUser( | |
firstName: "${createUser.firstName}" | |
lastName: "${createUser.lastName}" | |
enabled: true | |
locked: false | |
email: "${createUser.email}" | |
password: "${createUser.password}" | |
phoneNumber: "${createUser.phoneNumber}" | |
birthday: "${createUser.birthday}" | |
gendre: "${createUser.gendre}" | |
) { | |
id | |
firstName | |
lastName | |
enabled | |
locked | |
password | |
phoneNumber | |
birthday | |
gendre | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data.createUser; | |
id = data.id; | |
expect(data.firstName).toBe(createUser.firstName); | |
expect(data.lastName).toBe(createUser.lastName); | |
expect(data.enabled).toBe(true); | |
expect(data.locked).toBe(false); | |
expect(data.email).toBe(createUser.email); | |
expect(data.password).toBe(createUser.password); | |
expect(data.phoneNumber).toBe(createUser.phoneNumber); | |
expect(data.birthday).toBe('1969-11-02T00:00:00.000Z'); | |
expect(data.gendre).toBe(createUser.gendre); | |
}) | |
}); | |
it('Update User', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `mutation { | |
updateUser( | |
id: "${id}" | |
firstName: "${updateUser.firstName}" | |
lastName: "${updateUser.lastName}" | |
enabled: false | |
locked: true | |
email: "${updateUser.email}" | |
password: "${updateUser.password}" | |
phoneNumber: "${updateUser.phoneNumber}" | |
birthday: "${updateUser.birthday}" | |
gendre: "${updateUser.gendre}" | |
) { | |
id | |
firstName | |
lastName | |
enabled | |
locked | |
password | |
phoneNumber | |
birthday | |
gendre | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data.updateUser; | |
expect(data.firstName).toBe(updateUser.firstName); | |
expect(data.lastName).toBe(updateUser.lastName); | |
expect(data.enabled).toBe(false); | |
expect(data.locked).toBe(true); | |
expect(data.email).toBe(updateUser.email); | |
expect(data.password).toBe(updateUser.password); | |
expect(data.phoneNumber).toBe(updateUser.phoneNumber); | |
expect(data.birthday).toBe('1975-04-07T00:00:00.000Z'); | |
expect(data.gendre).toBe(updateUser.gendre); | |
}) | |
}); | |
it('Get User', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `{ | |
User(id: "${id}") { | |
id | |
firstName | |
lastName | |
enabled | |
locked | |
password | |
phoneNumber | |
birthday | |
gendre | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data.User; | |
expect(data.firstName).toBe(updateUser.firstName); | |
expect(data.lastName).toBe(updateUser.lastName); | |
expect(data.enabled).toBe(false); | |
expect(data.locked).toBe(true); | |
expect(data.email).toBe(updateUser.email); | |
expect(data.password).toBe(updateUser.password); | |
expect(data.phoneNumber).toBe(updateUser.phoneNumber); | |
expect(data.birthday).toBe('1975-04-07T00:00:00.000Z'); | |
expect(data.gendre).toBe(updateUser.gendre); | |
}) | |
}); | |
it('All Users', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `{ | |
allUsers{ | |
id | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data.allUsers; | |
expect(data).toHaveLength(1); | |
}) | |
}); | |
it('All Users Meta', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `{ | |
_allUsersMeta{ | |
count | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data._allUsersMeta; | |
expect(data.count).toBe(1); | |
}) | |
}); | |
it('Delete User', () => { | |
return request(app.getHttpServer()) | |
.post('/graphql') | |
.send({ | |
query: `mutation { | |
deleteUser(id: "${id}") { | |
id | |
firstName | |
lastName | |
enabled | |
locked | |
password | |
phoneNumber | |
birthday | |
gendre | |
} | |
}`, | |
}) | |
.expect(200) | |
.expect(({ body }) => { | |
const data = body.data.deleteUser; | |
expect(data.firstName).toBe(updateUser.firstName); | |
expect(data.lastName).toBe(updateUser.lastName); | |
expect(data.enabled).toBe(false); | |
expect(data.locked).toBe(true); | |
expect(data.email).toBe(updateUser.email); | |
expect(data.password).toBe(updateUser.password); | |
expect(data.phoneNumber).toBe(updateUser.phoneNumber); | |
expect(data.birthday).toBe('1975-04-07T00:00:00.000Z'); | |
expect(data.gendre).toBe(updateUser.gendre); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment