Add rpc/TeamService.test.ts

This commit is contained in:
Jerko Steiner 2019-09-07 19:44:29 +07:00
parent b813916c19
commit f509004dac
3 changed files with 126 additions and 1 deletions

View File

@ -1,6 +1,7 @@
// import {ITeam} from './ITeam'
import {Team} from './entities'
import {IUserInTeam} from './IUserInTeam'
import { keys } from 'ts-transformer-keys'
export interface ITeamAddUserParams {
teamId: number
@ -44,3 +45,5 @@ export interface ITeamService {
// TODO add other methods
}
export const TeamServiceMethods = keys<ITeamService>()

View File

@ -0,0 +1,122 @@
import { team as Team } from '@rondo.dev/common'
import { test } from '../test'
describe('team', () => {
test.withDatabase()
// const t = test.request('/api')
let mainUserId: number
let headers: Record<string, string> = {}
beforeEach(async () => {
const session = await test.registerAccount()
mainUserId = session.userId
headers = session.headers
})
const getClient = () =>
test.rpc<Team.ITeamService>(
'/rpc/teamService',
Team.TeamServiceMethods,
headers,
)
describe('create', () => {
it('creates a new team', async () => {
const team = await getClient().create({
name: 'test',
})
expect(team.name).toEqual('test')
})
})
describe('findOne', () => {
it('retrieves a team by id', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const team2 = await client.findOne(team.id)
expect(team2).toEqual(team)
})
})
describe('update', () => {
it('updates a team name', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const team2 = await client.update({
id: team.id,
name: 'test2',
})
expect(team2.name).toEqual('test2')
})
})
describe('remove', () => {
it('removes a team by id', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
await client.remove({id: team.id})
const team2 = await client.findOne(team.id)
expect(team2).toBe(undefined)
})
})
describe('find', () => {
it('retrieves all teams belonging to current user', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const teams = await client.find()
expect(teams.map(myTeam => ({teamId: myTeam.id})))
.toContainEqual({
teamId: team.id,
})
})
})
describe('addUser', () => {
it('adds a user to the team', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const teamId = team.id
const {userId} = await test.registerAccount('test2@user.com')
await client.addUser({userId, teamId, roleId: 1})
})
})
describe('removeUser', () => {
it('removes the user from the team', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const teamId = team.id
const {userId} = await test.registerAccount('test2@user.com')
await client.addUser({userId, teamId, roleId: 1})
await client.removeUser({userId, teamId, roleId: 1})
})
})
describe('findUsers', () => {
it('fetches team members user info', async () => {
const client = getClient()
const team = await client.create({name: 'test'})
const teamId = team.id
const {userId} = await test.registerAccount('test2@user.com')
await client.addUser({userId, teamId, roleId: 1})
const users = await client.findUsers(teamId)
expect(users.length).toBe(2)
expect(users).toEqual([{
teamId,
userId: mainUserId,
displayName: jasmine.any(String),
roleId: 1,
roleName: 'ADMIN',
}, {
teamId,
userId,
displayName: jasmine.any(String),
roleId: 1,
roleName: 'ADMIN',
}])
})
})
})

View File

@ -39,7 +39,7 @@ export class TeamService implements RPC<t.ITeamService> {
roleId: 1,
})
return team
return (await this.findOne(context, team.id))!
}
async remove(context: IContext, {id}: t.ITeamRemoveParams) {