Completed
Push — master ( 1cd4b4...40e33e )
by Jean
54s
created

utils.test.js ➔ ... ➔ sinon.stub   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 4
b 0
f 0
1
import chai from 'chai';
2
import chaiaspromised from 'chai-as-promised';
3
import mockfs from 'mock-fs';
4
import sinon from 'sinon';
5
import sinonchai from 'sinon-chai';
6
import mute from 'mute';
7
8
import cgapi from '../src/codingame-api.js';
9
import configure from '../src/configure.js';
10
11
import utils from '../src/utils.js';
12
13
let expect = chai.expect;
14
chai.use(chaiaspromised);
15
chai.use(sinonchai);
16
17
describe(`[module] utils`, function() {
18
	describe(`[method] kill`, function() {
19
		let exit;
20
		beforeEach(function() {
21
			exit = sinon.stub(process, `exit`);
22
		});
23
		afterEach(function() {
24
			exit.restore();
25
		});
26
		it(`should call process.exit with an negative value`, mute(function() {
27
			let message = `Some error message`;
28
			utils.kill(new Error(message));
29
			expect(exit).to.have.been.calledOnce;
0 ignored issues
show
introduced by
The result of the property access to expect(exit).to.have.been.calledOnce is not used.
Loading history...
30
			expect(exit.getCall(0).args[0]).to.be.below(0);
31
		}));
32
	});
33
	describe(`[method] login`, function() {
34
		let sandbox;
35
		beforeEach(function() {
36
			sandbox = sinon.sandbox.create();
37
		})
38
		afterEach(function() {
39
			sandbox.restore();
40
		});
41
		it(`should resolve if login is successfull`, function() {
42
			let login = sinon.stub(cgapi, `login`, function() {return Promise.resolve(true)});
43
			let get = sinon.stub(configure, `get`, function(property) {return Promise.resolve(property);});
44
			let log = utils.login(`username`, `password`)
45
			let calls = log.then(function() {
46
				expect(login).to.have.been.calledOnce;
0 ignored issues
show
introduced by
The result of the property access to expect(login).to.have.been.calledOnce is not used.
Loading history...
47
				expect(get).to.have.been.calledTwice;
0 ignored issues
show
introduced by
The result of the property access to expect(get).to.have.been.calledTwice is not used.
Loading history...
48
				return Promise.resolve(true);
49
			});
50
			return Promise.all([
51
				expect(log).to.eventually.be.fullfilled,
52
				calls
53
			]);
54
		});
55
		it(`should reject after 3 tries if authentication failed`, mute(function() {
56
			login = sandbox.stub(cgapi, `login`, function() {
0 ignored issues
show
Bug introduced by
The variable login seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.login.
Loading history...
57
				return Promise.resolve({
58
					"error": new Error(`Cannot authenticate`)
59
				});
60
			});
61
			get = sandbox.stub(configure, `get`, function(property) {return Promise.resolve(property);});
0 ignored issues
show
Bug introduced by
The variable get seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.get.
Loading history...
62
			let log = utils.login(`username`, `password`)
63
			let calls = log.catch(function() {
64
				expect(login).to.have.been.calledThrice;
0 ignored issues
show
introduced by
The result of the property access to expect(login).to.have.been.calledThrice is not used.
Loading history...
65
				return Promise.resolve(true);
66
			});
67
			return Promise.all([
68
				expect(log).to.eventually.be.rejected,
69
				calls
70
			]);
71
		}));
72
	});
73
	describe(`[method] tests`, function() {
74
	});
75
});
76