mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
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:
parent
60c473bb94
commit
d1ecac4580
@ -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>' }
|
||||
|
||||
@ -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 = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user