mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-05 01:32:47 +00:00
Switch to Calc's simplified test spec
This commit is contained in:
@@ -2,54 +2,27 @@ import mongoose from 'mongoose';
|
|||||||
import dbCheck from './dbCheck.js';
|
import dbCheck from './dbCheck.js';
|
||||||
import config from '../config.js';
|
import config from '../config.js';
|
||||||
|
|
||||||
describe('database check middleware', ()=>{
|
describe('dbCheck middleware', ()=>{
|
||||||
let request;
|
const next = jest.fn();
|
||||||
let response;
|
|
||||||
let next;
|
|
||||||
|
|
||||||
beforeEach(()=>{
|
afterEach(()=>jest.clearAllMocks());
|
||||||
request = {
|
|
||||||
get : function(key) {
|
|
||||||
return this[key];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
response = {
|
|
||||||
status : jest.fn(()=>response),
|
|
||||||
send : jest.fn(()=>{})
|
|
||||||
};
|
|
||||||
next = jest.fn();
|
|
||||||
|
|
||||||
// Mock the Config module
|
|
||||||
jest.mock('../config.js');
|
|
||||||
config.get = jest.fn((param)=>{
|
|
||||||
// The requested key name will be reflected to the output
|
|
||||||
return param;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(()=>{
|
|
||||||
jest.clearAllMocks();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return 503 if readystate != 1', ()=>{
|
|
||||||
const dbState = mongoose.connection.readyState;
|
|
||||||
|
|
||||||
mongoose.connection.readyState = 99;
|
|
||||||
|
|
||||||
expect(()=>{dbCheck(request, response);}).toThrow(new Error('Unable to connect to database'));
|
|
||||||
|
|
||||||
mongoose.connection.readyState = dbState;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should call next if readystate == 1', ()=>{
|
|
||||||
const dbState = mongoose.connection.readyState;
|
|
||||||
|
|
||||||
mongoose.connection.readyState = 1;
|
|
||||||
|
|
||||||
dbCheck(request, response, next);
|
|
||||||
|
|
||||||
mongoose.connection.readyState = dbState;
|
|
||||||
|
|
||||||
|
it('should skip check in test mode', ()=>{
|
||||||
|
config.get = jest.fn(()=>'test');
|
||||||
|
expect(()=>dbCheck({}, {}, next)).not.toThrow();
|
||||||
expect(next).toHaveBeenCalled();
|
expect(next).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
it('should call next if readyState == 1', ()=>{
|
||||||
|
config.get = jest.fn(()=>'production');
|
||||||
|
mongoose.connection.readyState = 1;
|
||||||
|
dbCheck({}, {}, next);
|
||||||
|
expect(next).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw if readyState != 1', ()=>{
|
||||||
|
config.get = jest.fn(()=>'production');
|
||||||
|
mongoose.connection.readyState = 99;
|
||||||
|
expect(()=>dbCheck({}, {}, next)).toThrow(/Unable to connect/);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user