Add test for moving to the top

This commit is contained in:
Hongbo Wu 2022-07-28 22:19:03 +08:00
parent ace5628c11
commit 7c73982fe3
2 changed files with 29 additions and 4 deletions

View file

@ -465,6 +465,7 @@ export const moveLabelResolver = authorized<
}
newPosition = afterLabel.position
}
const moveUp = newPosition < oldPosition
// move label to the new position
const updated = await AppDataSource.transaction(async (t) => {
@ -474,10 +475,13 @@ export const moveLabelResolver = authorized<
const updated = await t.getRepository(Label).update(
{
user: { id: uid },
position: Between(oldPosition, newPosition),
position: Between(
Math.min(newPosition, oldPosition),
Math.max(newPosition, oldPosition)
),
},
{
position: () => `position + ${newPosition < oldPosition ? 1 : -1}`,
position: () => `position + ${moveUp ? 1 : -1}`,
}
)
if (!updated.affected) {

View file

@ -646,7 +646,7 @@ describe('Labels API', () => {
})
describe('Move label', () => {
const query = (labelId: string, afterLabelId?: string): string => `
const query = (labelId: string, afterLabelId: string): string => `
mutation {
moveLabel(
input: {
@ -691,6 +691,12 @@ describe('Labels API', () => {
afterLabelId = labels[4].id
})
after(async () => {
await graphqlRequest(query(labelId, labels[0].id), authToken).expect(
200
)
})
it('moves label after the pointed label', async () => {
const res = await graphqlRequest(
query(labelId, afterLabelId),
@ -712,13 +718,28 @@ describe('Labels API', () => {
})
})
context('when afterLabelId is null', () => {
before(() => {
labelId = labels[4].id
})
it('moves the label to the top', async () => {
const res = await graphqlRequest(query(labelId, ''), authToken).expect(
200
)
expect(res.body.data.moveLabel.label.position).to.eql(1)
})
})
context('when label not exist', () => {
before(() => {
labelId = generateFakeUuid()
})
it('returns error code NOT_FOUND', async () => {
const res = await graphqlRequest(query(labelId), authToken).expect(200)
const res = await graphqlRequest(query(labelId, ''), authToken).expect(
200
)
expect(res.body.data.moveLabel.errorCodes).to.eql(['NOT_FOUND'])
})
})