-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from timothee-durand/dev
Add new chat feed mock
- Loading branch information
Showing
5 changed files
with
158 additions
and
68 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
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,33 +1,56 @@ | ||
import request from 'supertest'; | ||
import app from '../../../../app'; | ||
import request from 'supertest' | ||
import app from '../../../../app' | ||
// const app = 'http://localhost:3000'; | ||
|
||
const testDefaultRoutes = (response: request.Response): void => { | ||
expect(response.body.messages.length).toBe(1000) | ||
expect(response.body.messages[0]).toHaveProperty('id') | ||
expect(response.body.messages[0]).toHaveProperty('createdAt') | ||
expect(response.body.messages[0]).toHaveProperty('message') | ||
expect(response.body.messages[0]).toHaveProperty('userId') | ||
} | ||
|
||
const testParametizedRoutes = (response: request.Response, userCount: number, messageCount: number): void => { | ||
const userSet = new Set() | ||
|
||
response.body.users.forEach((element) => { | ||
userSet.add(element.userId) | ||
}) | ||
|
||
expect(response.body.messages.length).toBe(messageCount) | ||
expect(userSet.size).toBe(userCount) | ||
} | ||
|
||
describe.only('chat api endpoints', () => { | ||
describe('GET /chat/random', () => { | ||
it('should return a random chat snapshot with 1000 messages', async () => { | ||
const response = await request(app).get(`/chat/random`); | ||
expect(response.body.messages.length).toBe(1000); | ||
expect(response.body.messages[0]).toHaveProperty('id'); | ||
expect(response.body.messages[0]).toHaveProperty('createdAt'); | ||
expect(response.body.messages[0]).toHaveProperty('message'); | ||
expect(response.body.messages[0]).toHaveProperty('userId'); | ||
}); | ||
}); | ||
|
||
describe('GET /chat/random/:userCount/:messageCount', () => { | ||
it('should return a chat with the correct message and user count', async () => { | ||
const userCount = 5; | ||
const messageCount = 100; | ||
const response = await request(app).get(`/chat/random/${userCount}/${messageCount}`); | ||
|
||
const userSet = new Set(); | ||
|
||
response.body.users.forEach((element) => { | ||
userSet.add(element.userId); | ||
}); | ||
|
||
expect(response.body.messages.length).toBe(messageCount); | ||
expect(userSet.size).toBe(userCount); | ||
}); | ||
}); | ||
}); | ||
describe('GET /chat/random', () => { | ||
it('should return a random chat snapshot with 1000 messages', async () => { | ||
const response = await request(app).get('/chat/random') | ||
testDefaultRoutes(response) | ||
}) | ||
}) | ||
|
||
describe('GET /chat/random/various', () => { | ||
it('should return a random chat snapshot with 1000 messages', async () => { | ||
const response = await request(app).get('/chat/random/various') | ||
testDefaultRoutes(response) | ||
}) | ||
}) | ||
|
||
describe('GET /chat/random/:userCount/:messageCount', () => { | ||
it('should return a chat with the correct message and user count', async () => { | ||
const userCount = 5 | ||
const messageCount = 100 | ||
const response = await request(app).get(`/chat/random/${userCount}/${messageCount}`) | ||
testParametizedRoutes(response, userCount, messageCount) | ||
}) | ||
}) | ||
|
||
describe('GET /chat/random/various/:userCount/:messageCount', () => { | ||
it('should return a chat with the correct message and user count', async () => { | ||
const userCount = 5 | ||
const messageCount = 100 | ||
const response = await request(app).get(`/chat/random/various/${userCount}/${messageCount}`) | ||
testParametizedRoutes(response, userCount, messageCount) | ||
}) | ||
}) | ||
}) |
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,9 @@ | ||
import { generateRandomMessage } from '../../utils/generateRandomMessage' | ||
|
||
describe('generate random message', () => { | ||
it('should return a random string', () => { | ||
const res = generateRandomMessage() | ||
|
||
expect(typeof res).toBe('string') | ||
}) | ||
}) |
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,12 @@ | ||
import { faker } from '@faker-js/faker' | ||
import { getRandomArrayItem } from '../../../utils/arrays' | ||
|
||
const messageProviders: Array<() => string> = [ | ||
faker.lorem.sentence, | ||
faker.lorem.word, | ||
faker.lorem.paragraph | ||
] | ||
|
||
export const generateRandomMessage = (): string => { | ||
return getRandomArrayItem(messageProviders)() | ||
} |
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,22 +1,23 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import getRandomUsers from "../../users/utils/getRandomUsers"; | ||
import ChatMessage from "../consts/ChatMessage"; | ||
import ChatRandomResponse from "../consts/ChatRandomResponse"; | ||
import { faker } from '@faker-js/faker' | ||
import getRandomUsers from '../../users/utils/getRandomUsers' | ||
import ChatMessage from '../consts/ChatMessage' | ||
import ChatRandomResponse from '../consts/ChatRandomResponse' | ||
import { generateRandomMessage } from './generateRandomMessage' | ||
|
||
export const getRandomChat = (userCount : number, messageCount: number) : ChatRandomResponse => { | ||
const users = getRandomUsers(userCount); | ||
export const getRandomChat = (userCount: number, messageCount: number, various: boolean = false): ChatRandomResponse => { | ||
const users = getRandomUsers(userCount) | ||
|
||
const messages : ChatMessage[] = []; | ||
Array.from({length: messageCount}).forEach(() => { | ||
messages.push({ | ||
id: faker.datatype.uuid(), | ||
createdAt: faker.date.recent(faker.datatype.number({ min: 0, max: 30 })).toISOString(), | ||
message: faker.lorem.sentence(), | ||
userId: users[faker.datatype.number({ min: 0, max: userCount - 1 })].userId, | ||
}) | ||
const messages: ChatMessage[] = [] | ||
Array.from({ length: messageCount }).forEach(() => { | ||
messages.push({ | ||
id: faker.datatype.uuid(), | ||
createdAt: faker.date.recent(faker.datatype.number({ min: 0, max: 30 })).toISOString(), | ||
message: various ? generateRandomMessage() : faker.lorem.sentence(), | ||
userId: users[faker.datatype.number({ min: 0, max: userCount - 1 })].userId | ||
}) | ||
return { | ||
users, | ||
messages | ||
} | ||
}) | ||
return { | ||
users, | ||
messages | ||
} | ||
} |