summaryrefslogtreecommitdiff
path: root/spec/javascripts/image_diff/image_badge_spec.js
blob: 4d8c9f6623b571ffcf490163e8c3c505fd6667be (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
import ImageBadge from '~/image_diff/image_badge';
import imageDiffHelper from '~/image_diff/helpers/index';

describe('ImageBadge', () => {
  const noteId = 'noteId';
  const discussionId = 'discussionId';
  const options = {
    noteId,
    discussionId,
  };
  const meta = {
    x: 1,
    y: 1,
    width: 1,
    height: 1,
  };

  it('should save actual property', () => {
    const imageBadge = new ImageBadge(Object.assign({}, options, {
      actual: meta,
    }));

    const { actual } = imageBadge;
    expect(actual.x).toEqual(meta.x);
    expect(actual.y).toEqual(meta.y);
    expect(actual.width).toEqual(meta.width);
    expect(actual.height).toEqual(meta.height);
  });

  it('should save browser property', () => {
    const imageBadge = new ImageBadge(Object.assign({}, options, {
      browser: meta,
    }));

    const { browser } = imageBadge;
    expect(browser.x).toEqual(meta.x);
    expect(browser.y).toEqual(meta.y);
    expect(browser.width).toEqual(meta.width);
    expect(browser.height).toEqual(meta.height);
  });

  it('should save noteId', () => {
    const imageBadge = new ImageBadge(options);
    expect(imageBadge.noteId).toEqual(noteId);
  });

  it('should save discussionId', () => {
    const imageBadge = new ImageBadge(options);
    expect(imageBadge.discussionId).toEqual(discussionId);
  });

  describe('default values', () => {
    let imageBadge;

    beforeEach(() => {
      imageBadge = new ImageBadge(options);
    });

    it('should return defaultMeta if actual property is not provided', () => {
      const { actual } = imageBadge;
      expect(actual.x).toEqual(0);
      expect(actual.y).toEqual(0);
      expect(actual.width).toEqual(0);
      expect(actual.height).toEqual(0);
    });

    it('should return defaultMeta if browser property is not provided', () => {
      const { browser } = imageBadge;
      expect(browser.x).toEqual(0);
      expect(browser.y).toEqual(0);
      expect(browser.width).toEqual(0);
      expect(browser.height).toEqual(0);
    });
  });

  describe('imageEl property is provided and not browser property', () => {
    beforeEach(() => {
      spyOn(imageDiffHelper, 'resizeCoordinatesToImageElement').and.returnValue(true);
    });

    it('should generate browser property', () => {
      const imageBadge = new ImageBadge(Object.assign({}, options, {
        imageEl: document.createElement('img'),
      }));

      expect(imageDiffHelper.resizeCoordinatesToImageElement).toHaveBeenCalled();
      expect(imageBadge.browser).toEqual(true);
    });
  });
});