Skip to content

Commit

Permalink
Merge pull request #976 from parlemonde/david/update-tests
Browse files Browse the repository at this point in the history
Update server unit tests with supertest
  • Loading branch information
DavidRobertAnsart authored Sep 1, 2024
2 parents de52f7c + 776a513 commit 43ff2df
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 466 deletions.
159 changes: 0 additions & 159 deletions server/__tests__/fileUpload.test.ts

This file was deleted.

151 changes: 26 additions & 125 deletions server/__tests__/game.test.ts
Original file line number Diff line number Diff line change
@@ -1,137 +1,38 @@
import type { Request as ExpressRequest, Response as ExpressResponse } from 'express';
import './mocks';

import supertest from 'supertest';

import { getApp } from '../app';
import { Activity } from '../entities/activity';
import { User } from '../entities/user';
import { Village } from '../entities/village';
import { appDataSource, fakeUser, loginUser } from './mock';

// Mock connection to database to avoid error message in console.
jest.mock('../utils/database', () => ({
connection: Promise.resolve(),
}));
// Mock nodemailer to avoid message in console.
jest.mock('../emails/nodemailer', () => ({
__esModule: true,
getNodeMailer: async () => ({
t: null,
}),
}));

// Mock frontend NextJS library. We don't need it for testing.
jest.mock('next', () => ({
__esModule: true,
default: () => ({
getRequestHandler: () => (_req: ExpressRequest, res: ExpressResponse) => {
res.sendJSON({ isFrontend: true });
},
prepare: () => Promise.resolve(),
}),
}));

jest.mock('../authentication/login', () => ({
__esModule: true,
login: async () => ({
user: fakeUser,
}),
}));

export const activity = {
id: 18,
type: 4,
phase: 2,
status: 0,
data: {
game1: { video: 'https://vimeo.com/36273186', gameId: 1, origine: 'b', signification: 'a', fakeSignification1: 'c', fakeSignification2: 'd' },
game2: {
video: 'https://vimeo.com/134108914',
gameId: 2,
origine: 'origin',
signification: 'bubble',
fakeSignification1: 'test',
fakeSignification2: 'test',
},
game3: {
video: 'https://vimeo.com/221414987',
gameId: 3,
origine: 'origin',
signification: 'real',
fakeSignification1: 'fake 1',
fakeSignification2: 'fake 2',
},
draftUrl: '/creer-un-jeu/mimique/3',
presentation: 'La classe de CE 2 à Paris',
},
content: [{ id: 0, type: 'text', value: '' }],
userId: 6,
villageId: 1,
createDate: Date,
updateDate: Date,
deleteDate: null,
subType: 0,
responseActivityId: null,
responseType: null,
isPinned: 0,
displayUser: 0,
games: [],
images: [],
user: User,
village: Village,
responseActivity: Activity,
};

export const mimique = {
id: 1,
type: 0,
content: { fakeSignification1: 'c', fakeSignification2: 'd', origine: 'b', signification: 'a', video: 'https://vimeo.com/36273186' },
userId: 6,
villageId: 1,
activityId: 18,
value: '',
};
let accessToken = '';

describe('game', () => {
beforeAll(() => {
return appDataSource.initialize();
// Get the access token for the teacher
// to be able to make authenticated requests
beforeAll(async () => {
const app = await getApp();
const response = await supertest(app)
.post('/login')
.set('Accept', 'application/json')
.send({
email: '[email protected]',
password: 'helloWorld*',
})
.expect(200);
accessToken = response.body.accessToken;
});
afterAll(() => {
// return conn.close();
return appDataSource.destroy();

it('Get list of games (should be empty)', async () => {
const app = await getApp();
const response = await supertest(app).get(`/api/games`).set('authorization', `Bearer ${accessToken}`).expect(200);
expect(response.body).toEqual([]);
});

describe('get games', () => {
const auth = { token: '' };
beforeAll(async () => {
return loginUser(auth);
});
// TODO: Add post request to create a game

describe('given game does not exist', () => {
it('should return 404', async () => {
const gameId = 'game-123';
try {
const app = await getApp();
await supertest(app).get(`/api/games/${gameId}`).expect(404);
} catch (e) {
expect(400);
}
});
});
// TODO: Add get request to get the created game

describe('given game does exist', () => {
it('should return 200 status and the game', async () => {
const game = mimique;
try {
const app = await getApp();
const { body, statusCode } = await supertest(app)
.get(`/api/games/${game.id}`)
.set({ Authorization: 'bearer ' + auth.token, 'Content-Type': 'application/json' });
expect(statusCode).toBe(200);
expect(body.id).toEqual(game.id);
} catch (e) {
expect(400);
}
});
});
});
// TODO: Add put request to update the created game

// TODO: Add delete request to delete the created game
});
58 changes: 0 additions & 58 deletions server/__tests__/mock.ts

This file was deleted.

Loading

0 comments on commit 43ff2df

Please sign in to comment.