Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 5.2: Test addMessage() mutation
- Loading branch information
Showing
4 changed files
with
154 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,83 @@ | ||
export const messages = [ | ||
{ | ||
id: '1', | ||
content: 'You on your way?', | ||
createdAt: new Date(new Date('1-1-2019').getTime() - 60 * 1000 * 1000), | ||
}, | ||
{ | ||
id: '2', | ||
content: "Hey, it's me", | ||
createdAt: new Date(new Date('1-1-2019').getTime() - 2 * 60 * 1000 * 1000), | ||
}, | ||
{ | ||
id: '3', | ||
content: 'I should buy a boat', | ||
createdAt: new Date(new Date('1-1-2019').getTime() - 24 * 60 * 1000 * 1000), | ||
}, | ||
{ | ||
id: '4', | ||
content: 'This is wicked good ice cream.', | ||
createdAt: new Date( | ||
new Date('1-1-2019').getTime() - 14 * 24 * 60 * 1000 * 1000 | ||
), | ||
}, | ||
]; | ||
export type Message = { | ||
id: string; | ||
content: string; | ||
createdAt: Date; | ||
}; | ||
|
||
export const chats = [ | ||
{ | ||
id: '1', | ||
name: 'Ethan Gonzalez', | ||
picture: 'https://randomuser.me/api/portraits/thumb/men/1.jpg', | ||
messages: ['1'], | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Bryan Wallace', | ||
picture: 'https://randomuser.me/api/portraits/thumb/men/2.jpg', | ||
messages: ['2'], | ||
}, | ||
{ | ||
id: '3', | ||
name: 'Avery Stewart', | ||
picture: 'https://randomuser.me/api/portraits/thumb/women/1.jpg', | ||
messages: ['3'], | ||
}, | ||
{ | ||
id: '4', | ||
name: 'Katie Peterson', | ||
picture: 'https://randomuser.me/api/portraits/thumb/women/2.jpg', | ||
messages: ['4'], | ||
}, | ||
]; | ||
export type Chat = { | ||
id: string; | ||
name: string; | ||
picture: string; | ||
messages: string[]; | ||
}; | ||
|
||
export const messages: Message[] = []; | ||
export const chats: Chat[] = []; | ||
|
||
export const resetDb = () => { | ||
messages.splice( | ||
0, | ||
Infinity, | ||
...[ | ||
{ | ||
id: '1', | ||
content: 'You on your way?', | ||
createdAt: new Date(new Date('1-1-2019').getTime() - 60 * 1000 * 1000), | ||
}, | ||
{ | ||
id: '2', | ||
content: "Hey, it's me", | ||
createdAt: new Date( | ||
new Date('1-1-2019').getTime() - 2 * 60 * 1000 * 1000 | ||
), | ||
}, | ||
{ | ||
id: '3', | ||
content: 'I should buy a boat', | ||
createdAt: new Date( | ||
new Date('1-1-2019').getTime() - 24 * 60 * 1000 * 1000 | ||
), | ||
}, | ||
{ | ||
id: '4', | ||
content: 'This is wicked good ice cream.', | ||
createdAt: new Date( | ||
new Date('1-1-2019').getTime() - 14 * 24 * 60 * 1000 * 1000 | ||
), | ||
}, | ||
] | ||
); | ||
|
||
chats.splice( | ||
0, | ||
Infinity, | ||
...[ | ||
{ | ||
id: '1', | ||
name: 'Ethan Gonzalez', | ||
picture: 'https://randomuser.me/api/portraits/thumb/men/1.jpg', | ||
messages: ['1'], | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Bryan Wallace', | ||
picture: 'https://randomuser.me/api/portraits/thumb/men/2.jpg', | ||
messages: ['2'], | ||
}, | ||
{ | ||
id: '3', | ||
name: 'Avery Stewart', | ||
picture: 'https://randomuser.me/api/portraits/thumb/women/1.jpg', | ||
messages: ['3'], | ||
}, | ||
{ | ||
id: '4', | ||
name: 'Katie Peterson', | ||
picture: 'https://randomuser.me/api/portraits/thumb/women/2.jpg', | ||
messages: ['4'], | ||
}, | ||
] | ||
); | ||
}; | ||
|
||
resetDb(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Mutation.addMessage should add message to specified chat 1`] = ` | ||
Object { | ||
"addMessage": Object { | ||
"content": "Hello World", | ||
"id": "5", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`Mutation.addMessage should add message to specified chat 2`] = ` | ||
Object { | ||
"chat": Object { | ||
"id": "1", | ||
"lastMessage": Object { | ||
"content": "Hello World", | ||
"id": "5", | ||
}, | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createTestClient } from 'apollo-server-testing'; | ||
import { ApolloServer, gql } from 'apollo-server-express'; | ||
import schema from '../../schema'; | ||
import { resetDb } from '../../db'; | ||
|
||
describe('Mutation.addMessage', () => { | ||
beforeEach(resetDb); | ||
|
||
it('should add message to specified chat', async () => { | ||
const server = new ApolloServer({ schema }); | ||
|
||
const { query, mutate } = createTestClient(server); | ||
|
||
const addMessageRes = await mutate({ | ||
variables: { chatId: '1', content: 'Hello World' }, | ||
mutation: gql` | ||
mutation AddMessage($chatId: ID!, $content: String!) { | ||
addMessage(chatId: $chatId, content: $content) { | ||
id | ||
content | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
expect(addMessageRes.data).toBeDefined(); | ||
expect(addMessageRes.errors).toBeUndefined(); | ||
expect(addMessageRes.data).toMatchSnapshot(); | ||
|
||
const getChatRes = await query({ | ||
variables: { chatId: '1' }, | ||
query: gql` | ||
query GetChat($chatId: ID!) { | ||
chat(chatId: $chatId) { | ||
id | ||
lastMessage { | ||
id | ||
content | ||
} | ||
} | ||
} | ||
`, | ||
}); | ||
|
||
expect(getChatRes.data).toBeDefined(); | ||
expect(getChatRes.errors).toBeUndefined(); | ||
expect(getChatRes.data).toMatchSnapshot(); | ||
}); | ||
}); |