summaryrefslogtreecommitdiff
path: root/spec/javascripts/subbable_resource_spec.js.es6
blob: 6a70dd856a711e656a5a54bf17fa57b34f183ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* eslint-disable max-len, arrow-parens, comma-dangle, no-plusplus */

//= vue
//= vue-resource
//= require jquery
//= require subbable_resource

/*
* Test that each rest verb calls the publish and subscribe function and passes the correct value back
*
*
* */
((global) => {
  describe('Subbable Resource', function () {
    describe('PubSub', function () {
      beforeEach(function () {
        this.MockResource = new global.SubbableResource('https://example.com');
      });
      it('should successfully add a single subscriber', function () {
        const callback = () => {};
        this.MockResource.subscribe(callback);

        expect(this.MockResource.subscribers.length).toBe(1);
        expect(this.MockResource.subscribers[0]).toBe(callback);
      });

      it('should successfully add multiple subscribers', function () {
        const callbackOne = () => {};
        const callbackTwo = () => {};
        const callbackThree = () => {};

        this.MockResource.subscribe(callbackOne);
        this.MockResource.subscribe(callbackTwo);
        this.MockResource.subscribe(callbackThree);

        expect(this.MockResource.subscribers.length).toBe(3);
      });

      it('should successfully publish an update to a single subscriber', function () {
        const state = { myprop: 1 };

        const callbacks = {
          one: (data) => expect(data.myprop).toBe(2),
          two: (data) => expect(data.myprop).toBe(2),
          three: (data) => expect(data.myprop).toBe(2)
        };

        const spyOne = spyOn(callbacks, 'one');
        const spyTwo = spyOn(callbacks, 'two');
        const spyThree = spyOn(callbacks, 'three');

        this.MockResource.subscribe(callbacks.one);
        this.MockResource.subscribe(callbacks.two);
        this.MockResource.subscribe(callbacks.three);

        state.myprop++;

        this.MockResource.publish(state);

        expect(spyOne).toHaveBeenCalled();
        expect(spyTwo).toHaveBeenCalled();
        expect(spyThree).toHaveBeenCalled();
      });
    });
  });
})(window.gl || (window.gl = {}));