summaryrefslogtreecommitdiff
path: root/spec/javascripts/comment_type_toggle_spec.js
blob: 9a341ebd8118501db2e182b5f905c178ea6d65f0 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import CommentTypeToggle from '~/comment_type_toggle';
import * as dropLabSrc from '~/droplab/drop_lab';
import InputSetter from '~/droplab/plugins/input_setter';

describe('CommentTypeToggle', function () {
  describe('class constructor', function () {
    beforeEach(function () {
      this.dropdownTrigger = {};
      this.dropdownList = {};
      this.noteTypeInput = {};
      this.submitButton = {};
      this.closeButton = {};

      this.commentTypeToggle = new CommentTypeToggle({
        dropdownTrigger: this.dropdownTrigger,
        dropdownList: this.dropdownList,
        noteTypeInput: this.noteTypeInput,
        submitButton: this.submitButton,
        closeButton: this.closeButton,
      });
    });

    it('should set .dropdownTrigger', function () {
      expect(this.commentTypeToggle.dropdownTrigger).toBe(this.dropdownTrigger);
    });

    it('should set .dropdownList', function () {
      expect(this.commentTypeToggle.dropdownList).toBe(this.dropdownList);
    });

    it('should set .noteTypeInput', function () {
      expect(this.commentTypeToggle.noteTypeInput).toBe(this.noteTypeInput);
    });

    it('should set .submitButton', function () {
      expect(this.commentTypeToggle.submitButton).toBe(this.submitButton);
    });

    it('should set .closeButton', function () {
      expect(this.commentTypeToggle.closeButton).toBe(this.closeButton);
    });

    it('should set .reopenButton', function () {
      expect(this.commentTypeToggle.reopenButton).toBe(this.reopenButton);
    });
  });

  describe('initDroplab', function () {
    beforeEach(function () {
      this.commentTypeToggle = {
        dropdownTrigger: {},
        dropdownList: {},
        noteTypeInput: {},
        submitButton: {},
        closeButton: {},
        setConfig: () => {},
      };
      this.config = {};

      this.droplab = jasmine.createSpyObj('droplab', ['init']);

      spyOn(dropLabSrc, 'default').and.returnValue(this.droplab);
      spyOn(this.commentTypeToggle, 'setConfig').and.returnValue(this.config);

      CommentTypeToggle.prototype.initDroplab.call(this.commentTypeToggle);
    });

    it('should instantiate a DropLab instance', function () {
      expect(dropLabSrc.default).toHaveBeenCalled();
    });

    it('should set .droplab', function () {
      expect(this.commentTypeToggle.droplab).toBe(this.droplab);
    });

    it('should call .setConfig', function () {
      expect(this.commentTypeToggle.setConfig).toHaveBeenCalled();
    });

    it('should call DropLab.prototype.init', function () {
      expect(this.droplab.init).toHaveBeenCalledWith(
        this.commentTypeToggle.dropdownTrigger,
        this.commentTypeToggle.dropdownList,
        [InputSetter],
        this.config,
      );
    });
  });

  describe('setConfig', function () {
    describe('if no .closeButton is provided', function () {
      beforeEach(function () {
        this.commentTypeToggle = {
          dropdownTrigger: {},
          dropdownList: {},
          noteTypeInput: {},
          submitButton: {},
          reopenButton: {},
        };

        this.setConfig = CommentTypeToggle.prototype.setConfig.call(this.commentTypeToggle);
      });

      it('should not add .closeButton or .reopenButton related InputSetter config', function () {
        expect(this.setConfig).toEqual({
          InputSetter: [{
            input: this.commentTypeToggle.noteTypeInput,
            valueAttribute: 'data-value',
          }, {
            input: this.commentTypeToggle.submitButton,
            valueAttribute: 'data-submit-text',
          }],
        });
      });
    });

    describe('if no .reopenButton is provided', function () {
      beforeEach(function () {
        this.commentTypeToggle = {
          dropdownTrigger: {},
          dropdownList: {},
          noteTypeInput: {},
          submitButton: {},
          closeButton: {},
        };

        this.setConfig = CommentTypeToggle.prototype.setConfig.call(this.commentTypeToggle);
      });

      it('should not add .closeButton or .reopenButton related InputSetter config', function () {
        expect(this.setConfig).toEqual({
          InputSetter: [{
            input: this.commentTypeToggle.noteTypeInput,
            valueAttribute: 'data-value',
          }, {
            input: this.commentTypeToggle.submitButton,
            valueAttribute: 'data-submit-text',
          }],
        });
      });
    });
  });
});