UNPKG

2.68 kBJavaScriptView Raw
1const expect = require('chai').expect
2const nock = require('nock')
3
4describe('Dependency', () => {
5 let Dependency
6 let dependency
7
8 before(() => {
9 const CodependencyMock = require('./mocks/codependency')
10 const Injector = require('../lib/util/injector')
11
12 Dependency = require('../lib/dependency')(
13 Injector(CodependencyMock({
14 'node-fetch': require('node-fetch')
15 }))
16 )
17 })
18
19 beforeEach(() => {
20 dependency = Dependency({ name: 'test-dependency', url: 'http://example.com' })
21 })
22
23 it('should export a function', () => {
24 expect(Dependency).to.be.a('function')
25 })
26
27 describe('onHealthy()', () => {
28 it('should mark a dependency as healthy', () => {
29 dependency.onHealthy()
30
31 expect(dependency.healthy).to.equal(true)
32 })
33 })
34
35 describe('onUnhealthy()', () => {
36 it('should mark a dependency as unhealthy', () => {
37 dependency.onUnhealthy()
38
39 expect(dependency.healthy).to.equal(false)
40 })
41 })
42
43 describe('healthSummary', () => {
44 it('should return health summary of the dependency', () => {
45 dependency.onHealthy()
46
47 expect({
48 name: 'test-dependency',
49 healthy: true,
50 level: 'SOFT',
51 lastChecked: dependency.lastChecked
52 }).to.deep.equal(dependency.healthSummary)
53 })
54 })
55
56 describe('check()', () => {
57 it('should return a promise', () => {
58 nock('http://example.com')
59 .get('/')
60 .reply(200)
61
62 expect(dependency.check().then).to.be.a('function')
63 })
64
65 it('should use a HttpStrategy to check a dependency\'s health', () => {
66 nock('http://example.com')
67 .get('/')
68 .reply(200)
69
70 dependency
71 .check()
72 .catch((r) => {
73 expect({
74 name: 'test-dependency',
75 healthy: true,
76 dependency: 'SOFT',
77 lastChecked: r.lastChecked
78 }).to.deep.equal(r)
79 })
80
81 nock('http://example.com')
82 .get('/')
83 .reply(400)
84
85 dependency
86 .check()
87 .catch((r) => {
88 expect({
89 name: 'test-dependency',
90 healthy: false,
91 dependency: 'SOFT',
92 lastChecked: r.lastChecked
93 }).to.deep.equal(r)
94 })
95 })
96
97 it('should check a dependency\'s health if retries > 1', () => {
98 nock('http://example.com')
99 .get('/')
100 .reply(400)
101
102 dependency
103 .check(2)
104 .catch((r) => {
105 expect({
106 name: 'test-dependency',
107 healthy: true,
108 dependency: 'SOFT',
109 lastChecked: r.lastChecked
110 }).to.deep.equal(r)
111 })
112 })
113 })
114})