behaviours: add tests
This commit is contained in:
parent
7e1d7a703f
commit
f0207e61d7
16
src/behaviours/__tests__/direct-close.test.js
Normal file
16
src/behaviours/__tests__/direct-close.test.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import DirectCloseBehaviour from '../direct-close';
|
||||||
|
|
||||||
|
describe('DirectCloseBehaviour#run', () => {
|
||||||
|
|
||||||
|
it('should call onClose()', async () => {
|
||||||
|
const fn = jest.fn();
|
||||||
|
const behaviour = new DirectCloseBehaviour();
|
||||||
|
await behaviour.run({
|
||||||
|
remoteHost: 'test.com',
|
||||||
|
remotePort: 443,
|
||||||
|
onClose: fn
|
||||||
|
});
|
||||||
|
expect(fn).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
9
src/behaviours/__tests__/index.test.js
Normal file
9
src/behaviours/__tests__/index.test.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import {getBehaviourClassByName} from '..';
|
||||||
|
|
||||||
|
describe('getBehaviourClassByName', () => {
|
||||||
|
|
||||||
|
it('should throw when no behaviour found', () => {
|
||||||
|
expect(() => getBehaviourClassByName('')).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
36
src/behaviours/__tests__/random-timeout.test.js
Normal file
36
src/behaviours/__tests__/random-timeout.test.js
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import RandomTimeoutBehaviour from '../random-timeout';
|
||||||
|
|
||||||
|
describe('RandomTimeoutBehaviour#new', () => {
|
||||||
|
|
||||||
|
it('should throw when passing invalid params', () => {
|
||||||
|
expect(() => new RandomTimeoutBehaviour()).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({min: ''})).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({min: -1})).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({max: ''})).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({max: -1})).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({min: 1, max: 0})).toThrow();
|
||||||
|
expect(() => new RandomTimeoutBehaviour({})).not.toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set min and max to default', () => {
|
||||||
|
const behaviour = new RandomTimeoutBehaviour({});
|
||||||
|
expect(behaviour.min).toBe(10);
|
||||||
|
expect(behaviour.max).toBe(40);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('RandomTimeoutBehaviour#run', () => {
|
||||||
|
|
||||||
|
it('should call onClose()', async () => {
|
||||||
|
const fn = jest.fn();
|
||||||
|
const behaviour = new RandomTimeoutBehaviour({min: 0, max: 0});
|
||||||
|
await behaviour.run({
|
||||||
|
remoteHost: 'test.com',
|
||||||
|
remotePort: 443,
|
||||||
|
onClose: fn
|
||||||
|
});
|
||||||
|
expect(fn).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
40
src/behaviours/__tests__/redirect.test.js
Normal file
40
src/behaviours/__tests__/redirect.test.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import RedirectBehaviour from '../redirect';
|
||||||
|
|
||||||
|
describe('RedirectBehaviour#new', () => {
|
||||||
|
|
||||||
|
it('should throw when pass invalid params', () => {
|
||||||
|
expect(() => new RedirectBehaviour({})).toThrow();
|
||||||
|
expect(() => new RedirectBehaviour({host: ''})).toThrow();
|
||||||
|
expect(() => new RedirectBehaviour({host: 'test.com'})).toThrow();
|
||||||
|
expect(() => new RedirectBehaviour({host: 'test.com', port: -1})).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('RedirectBehaviour#run', () => {
|
||||||
|
|
||||||
|
it('should call connect() and setPresets()', async () => {
|
||||||
|
const mockWrite = jest.fn();
|
||||||
|
const mockConnect = jest.fn().mockImplementation(async () => {
|
||||||
|
return {
|
||||||
|
writable: true,
|
||||||
|
write: mockWrite
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const mockSetPresets = jest.fn();
|
||||||
|
|
||||||
|
const behaviour = new RedirectBehaviour({host: 'test.com', port: 443});
|
||||||
|
await behaviour.run({
|
||||||
|
action: {payload: {}},
|
||||||
|
remoteHost: '',
|
||||||
|
remotePort: 0,
|
||||||
|
connect: mockConnect,
|
||||||
|
setPresets: mockSetPresets
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockConnect).toHaveBeenCalled();
|
||||||
|
expect(mockSetPresets).toHaveBeenCalled();
|
||||||
|
expect(mockWrite).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
@ -13,7 +13,7 @@ export default class RandomTimeoutBehaviour {
|
|||||||
|
|
||||||
constructor({min, max}) {
|
constructor({min, max}) {
|
||||||
logger = Logger.getInstance();
|
logger = Logger.getInstance();
|
||||||
if (min !== undefined && !Number.isInteger(min)) {
|
if (min !== undefined) {
|
||||||
if (!Number.isInteger(min)) {
|
if (!Number.isInteger(min)) {
|
||||||
throw Error('\'min\' must be an integer');
|
throw Error('\'min\' must be an integer');
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ export default class RandomTimeoutBehaviour {
|
|||||||
throw Error('\'min\' must be greater than 0');
|
throw Error('\'min\' must be greater than 0');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (max !== undefined && !Number.isInteger(max)) {
|
if (max !== undefined) {
|
||||||
if (!Number.isInteger(max)) {
|
if (!Number.isInteger(max)) {
|
||||||
throw Error('\'max\' must be an integer');
|
throw Error('\'max\' must be an integer');
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user