Skip to content
Snippets Groups Projects
Commit a10e81cf authored by Jure's avatar Jure
Browse files

feat: add team relationship to user and test it

parent dfb2cce8
No related branches found
No related tags found
No related merge requests found
...@@ -9,9 +9,22 @@ process.env.NODE_CONFIG = `{"pubsweet":{ ...@@ -9,9 +9,22 @@ process.env.NODE_CONFIG = `{"pubsweet":{
const { model: Team } = require('../src') const { model: Team } = require('../src')
const { model: User } = require('@pubsweet/model-user') const { model: User } = require('@pubsweet/model-user')
const { dbCleaner } = require('pubsweet-server/test') const { dbCleaner } = require('pubsweet-server/test')
const createTeamWithMember = async () => {
const user = await new User({
email: 'some@example.com',
username: 'test',
}).save()
const newTeam = await new Team({ name: 'Test', role: 'testRole' }).save()
const team = await Team.query().findById(newTeam.id)
await team.$relatedQuery('members').relate(user.id)
return { user, team }
}
describe('Team', () => { describe('Team', () => {
beforeEach(async () => { beforeEach(async () => {
await dbCleaner() await dbCleaner()
...@@ -43,83 +56,29 @@ describe('Team', () => { ...@@ -43,83 +56,29 @@ describe('Team', () => {
}) })
it('can have some members', async () => { it('can have some members', async () => {
const user = await new User({ const newTeam = (await createTeamWithMember()).team
email: 'some@example.com', const team = await Team.query()
username: 'test',
}).save()
const newTeam = await new Team({ name: 'Test', role: 'testRole' }).save()
let team = await Team.query().findById(newTeam.id)
await team.$relatedQuery('members').relate(user.id)
team = await Team.query()
.findById(newTeam.id) .findById(newTeam.id)
.eager('members') .eager('members')
expect(team.members).toHaveLength(1) expect(team.members).toHaveLength(1)
}) })
// it('can be found by property', async () => { it('deletes memberships after team is deleted', async () => {
// await new Manuscript({ title: 'Test' }).save() const { team, user } = await createTeamWithMember()
// const team = await Manuscript.findOneByField('title', 'Test')
// expect(team.title).toEqual('Test') let foundUser = await User.query()
.findById(user.id)
// let manuscripts = await Manuscript.findByField('title', 'Test') .eager('teams')
// expect(manuscripts[0].title).toEqual('Test')
expect(foundUser.teams).toHaveLength(1)
// async function findMissing() {
// await Manuscript.findOneByField('title', 'Does not exist') await Team.query().deleteById(team.id)
// }
foundUser = await User.query()
// await expect(findMissing()).rejects.toThrow('Object not found') .findById(user.id)
.eager('teams')
// manuscripts = await Manuscript.findByField('title', 'Does not exist')
// expect(manuscripts).toEqual([]) expect(foundUser.teams).toHaveLength(0)
// }) })
// it('can not be saved with non-valid properties', async () => {
// async function createNonValidManuscript() {
// await new Manuscript({ mumbo: 'jumbo' }).save()
// }
// await expect(createNonValidManuscript()).rejects.toThrow(
// 'mumbo: is an invalid additional property',
// )
// })
// it('can assign to special properties', () => {
// const manuscript = new Manuscript()
// manuscript['#id'] = 'idref'
// })
// it('takes schema specified in config into account', async () => {
// const manuscript = new Manuscript({ configField: 'hello' })
// expect(manuscript.configField).toEqual('hello')
// })
// it('can save new entity with known ID', async () => {
// const id = '1838d074-fb9d-4ed6-9c63-39e6bc7429ce'
// const manuscript = await new Manuscript({ id }).save()
// expect(manuscript.id).toEqual(id)
// })
// it('old data does not overwrite new', async () => {
// // T0 - start time (A == B)
// let manuscriptA = await new Manuscript({ title: 'T0' }).save()
// expect(manuscriptA.title).toEqual('T0')
// const manuscriptB = await Manuscript.find(manuscriptA.id)
// // T1 - B is changed (not saved)
// manuscriptB.title = 'T1'
// // T2 - A is changed and saved
// manuscriptA.title = 'T2'
// manuscriptA = await manuscriptA.save()
// expect(manuscriptA.updated).not.toBe(manuscriptB.updated)
// // T4 - now save B, this should throw as `updated` is older than current.
// await expect(manuscriptB.save()).rejects.toThrow(
// 'Data Integrity Error property updated',
// )
// })
}) })
...@@ -24,6 +24,26 @@ class User extends BaseModel { ...@@ -24,6 +24,26 @@ class User extends BaseModel {
return 'users' return 'users'
} }
static get relationMappings() {
return {
teams: {
relation: BaseModel.ManyToManyRelation,
modelClass: require.resolve('@pubsweet/model-team/src/team'),
join: {
from: 'users.id',
through: {
modelClass: require.resolve(
'@pubsweet/model-team-member/src/team_member',
),
from: 'team_members.user_id',
to: 'team_members.team_id',
},
to: 'teams.id',
},
},
}
}
static get schema() { static get schema() {
return { return {
properties: { properties: {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment