Write tests to ensure createdAt is not deleted on update

- we had a silly serious bug by overwriting all attributes at the time
of UpdatePost
- tests in place to ensure we don't accidentally delete our createdAt
attribute
This commit is contained in:
mattwr18 2019-09-13 20:12:30 +02:00
parent 60c473bb94
commit d1ecac4580
2 changed files with 58 additions and 7 deletions

View File

@ -8,10 +8,7 @@ const driver = getDriver()
const neode = getNeode()
const factory = Factory()
let variables
let mutate
let authenticatedUser
let commentAuthor
let variables, mutate, authenticatedUser, commentAuthor, newlyCreatedComment
beforeAll(() => {
const { server } = createServer({
@ -57,7 +54,7 @@ const setupPostAndComment = async () => {
content: 'Post to be commented',
categoryIds: ['cat9'],
})
await factory.create('Comment', {
newlyCreatedComment = await factory.create('Comment', {
id: 'c456',
postId: 'p1',
author: commentAuthor,
@ -160,6 +157,8 @@ describe('UpdateComment', () => {
UpdateComment(content: $content, id: $id) {
id
content
createdAt
updatedAt
}
}
`
@ -200,6 +199,33 @@ describe('UpdateComment', () => {
)
})
it('updates a comment, but maintains non-updated attributes', async () => {
const expected = {
data: {
UpdateComment: {
id: 'c456',
content: 'The comment is updated',
createdAt: expect.any(String),
},
},
}
await expect(mutate({ mutation: updateCommentMutation, variables })).resolves.toMatchObject(
expected,
)
})
it('updates the updatedAt attribute', async () => {
newlyCreatedComment = await newlyCreatedComment.toJson()
const {
data: { UpdateComment },
} = await mutate({ mutation: updateCommentMutation, variables })
expect(newlyCreatedComment.updatedAt).toBeTruthy()
expect(Date.parse(newlyCreatedComment.updatedAt)).toEqual(expect.any(Number))
expect(UpdateComment.updatedAt).toBeTruthy()
expect(Date.parse(UpdateComment.updatedAt)).toEqual(expect.any(Number))
expect(newlyCreatedComment.updatedAt).not.toEqual(UpdateComment.updatedAt)
})
describe('if `content` empty', () => {
beforeEach(() => {
variables = { ...variables, content: ' <p> </p>' }

View File

@ -361,7 +361,7 @@ describe('CreatePost', () => {
})
describe('UpdatePost', () => {
let author
let author, newlyCreatedPost
const updatePostMutation = gql`
mutation($id: ID!, $title: String!, $content: String!, $categoryIds: [ID]) {
UpdatePost(id: $id, title: $title, content: $content, categoryIds: $categoryIds) {
@ -370,12 +370,14 @@ describe('UpdatePost', () => {
categories {
id
}
createdAt
updatedAt
}
}
`
beforeEach(async () => {
author = await factory.create('User', { slug: 'the-author' })
await factory.create('Post', {
newlyCreatedPost = await factory.create('Post', {
author,
id: 'p9876',
title: 'Old title',
@ -421,6 +423,29 @@ describe('UpdatePost', () => {
)
})
it('updates a post, but maintains non-updated attributes', async () => {
const expected = {
data: {
UpdatePost: { id: 'p9876', content: 'New content', createdAt: expect.any(String) },
},
}
await expect(mutate({ mutation: updatePostMutation, variables })).resolves.toMatchObject(
expected,
)
})
it('updates the updatedAt attribute', async () => {
newlyCreatedPost = await newlyCreatedPost.toJson()
const {
data: { UpdatePost },
} = await mutate({ mutation: updatePostMutation, variables })
expect(newlyCreatedPost.updatedAt).toBeTruthy()
expect(Date.parse(newlyCreatedPost.updatedAt)).toEqual(expect.any(Number))
expect(UpdatePost.updatedAt).toBeTruthy()
expect(Date.parse(UpdatePost.updatedAt)).toEqual(expect.any(Number))
expect(newlyCreatedPost.updatedAt).not.toEqual(UpdatePost.updatedAt)
})
describe('no new category ids provided for update', () => {
it('resolves and keeps current categories', async () => {
const expected = {