Skip to content

Commit

Permalink
Step 5.2: Test addMessage() mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB authored and Urigo committed May 20, 2020
1 parent edfc8a8 commit 2bbe9ff
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 51 deletions.
132 changes: 82 additions & 50 deletions db.ts
@@ -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();
2 changes: 1 addition & 1 deletion schema/resolvers.ts
Expand Up @@ -34,7 +34,7 @@ const resolvers = {
if (chatIndex === -1) return null;

const chat = chats[chatIndex];

const messagesIds = messages.map(currentMessage => Number(currentMessage.id));
const messageId = String(Math.max(...messagesIds) + 1);
const message = {
Expand Down
22 changes: 22 additions & 0 deletions tests/mutations/__snapshots__/addMessage.test.ts.snap
@@ -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",
},
},
}
`;
49 changes: 49 additions & 0 deletions tests/mutations/addMessage.test.ts
@@ -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();
});
});

0 comments on commit 2bbe9ff

Please sign in to comment.