mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-17 04:01:49 -04:00
refactor: remove sinon (#42337)
This commit is contained in:
committed by
GitHub
parent
7d1e9af9df
commit
802273cf8a
@@ -1,7 +1,5 @@
|
||||
/* global describe xdescribe it expect */
|
||||
/* global describe xdescribe it expect jest */
|
||||
import { first, find } from 'lodash';
|
||||
import sinon from 'sinon';
|
||||
import { mockReq, mockRes } from 'sinon-express-mock';
|
||||
|
||||
import {
|
||||
buildUserUpdate,
|
||||
@@ -23,6 +21,22 @@ import {
|
||||
mockCompletedChallenges
|
||||
} from './fixtures';
|
||||
|
||||
export const mockReq = opts => {
|
||||
const req = {};
|
||||
return { ...req, ...opts };
|
||||
};
|
||||
|
||||
export const mockRes = opts => {
|
||||
const res = {};
|
||||
res.status = jest.fn().mockReturnValue(res);
|
||||
res.json = jest.fn().mockReturnValue(res);
|
||||
res.redirect = jest.fn().mockReturnValue(res);
|
||||
res.set = jest.fn().mockReturnValue(res);
|
||||
res.clearCookie = jest.fn().mockReturnValue(res);
|
||||
res.cookie = jest.fn().mockReturnValue(res);
|
||||
return { ...res, ...opts };
|
||||
};
|
||||
|
||||
describe('boot/challenge', () => {
|
||||
xdescribe('backendChallengeCompleted', () => {});
|
||||
|
||||
@@ -221,37 +235,35 @@ describe('boot/challenge', () => {
|
||||
const validObjectId = '5c716d1801013c3ce3aa23e6';
|
||||
|
||||
it('declares a 403 for an invalid id in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: { id: 'not-a-real-id' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('declares a 403 for an invalid challengeType in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: { id: validObjectId, challengeType: 'ponyfoo' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('declares a 403 for an invalid solution in the body', () => {
|
||||
expect.assertions(3);
|
||||
expect.assertions(2);
|
||||
const req = mockReq({
|
||||
body: {
|
||||
id: validObjectId,
|
||||
@@ -260,13 +272,12 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(res.status.called).toBe(true);
|
||||
expect(res.status.getCall(0).args[0]).toBe(403);
|
||||
expect(next.called).toBe(false);
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls next if the body is valid', () => {
|
||||
@@ -278,11 +289,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls next if only the id is provided', () => {
|
||||
@@ -292,11 +303,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('can handle an "int" challengeType', () => {
|
||||
@@ -307,11 +318,11 @@ describe('boot/challenge', () => {
|
||||
}
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
|
||||
isValidChallengeCompletion(req, res, next);
|
||||
|
||||
expect(next.called).toBe(true);
|
||||
expect(next).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -337,10 +348,10 @@ describe('boot/challenge', () => {
|
||||
);
|
||||
const req = mockReq();
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
|
||||
expect(res.redirect.calledWith(mockLearnUrl));
|
||||
expect(res.redirect).toHaveBeenCalledWith(mockLearnUrl);
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -362,10 +373,10 @@ describe('boot/challenge', () => {
|
||||
user: mockUser
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
|
||||
expect(res.redirect.calledWith(expectedUrl)).toBe(true);
|
||||
expect(res.redirect).toHaveBeenCalledWith(expectedUrl);
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -386,10 +397,10 @@ describe('boot/challenge', () => {
|
||||
user: { ...mockUser, currentChallengeId: '' }
|
||||
});
|
||||
const res = mockRes();
|
||||
const next = sinon.spy();
|
||||
const next = jest.fn();
|
||||
await redirectToCurrentChallenge(req, res, next);
|
||||
const expectedUrl = `${mockHomeLocation}${firstChallengeUrl}`;
|
||||
expect(res.redirect.calledWith(expectedUrl)).toBe(true);
|
||||
expect(res.redirect).toHaveBeenCalledWith(expectedUrl);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user