aboutsummaryrefslogtreecommitdiffstats
path: root/spec/spec/image.js
blob: 9ea46108c0cf1de668da1e6055ce642a4bb70385 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
describe('Image', function() {
  var image

  beforeEach(function() {
    image = draw.image(imageUrl, 100, 100)
  })

  afterEach(function() {
    draw.clear()
  })


  describe('()', function() {
    it('should set width and height automatically if no size is given', function(done) {
      image = draw.image(imageUrl).loaded(function() {
        expect(image.node.getAttribute('height')).toBe('1')
        expect(image.node.getAttribute('width')).toBe('1')
        done()
      })
    })
    it('should set width and height if size is given', function(done) {
      image = draw.image(imageUrl, 100, 100).loaded(function() {
        expect(image.node.getAttribute('height')).toBe('100')
        expect(image.node.getAttribute('width')).toBe('100')
        done()
      })
    })
  })

  describe('loaded()', function() {
    beforeEach(function(done) {
      loadCb = {cb: function(){ done() }}
      errorCb = jasmine.createSpy('errorCb')
      spyOn(loadCb, 'cb').and.callThrough()
      image = draw.image(imageUrl, 100, 100).loaded(loadCb.cb).error(errorCb)
    })

    it('should set the load callback', function() {
      expect(image._loaded).toBe(loadCb.cb)
    })
    it('executes the load callback', function() {
      expect(loadCb.cb).toHaveBeenCalledWith({
        width: 1,
        height: 1,
        ratio: 1,
        url: jasmine.any(String)
      })
    })
    it('does not execute the error callback', function() {
      expect(errorCb).not.toHaveBeenCalled()
    })
  })

  describe('error()', function() {
    beforeEach(function(done) {
      loadCb = jasmine.createSpy('loadCb')
      errorCb = {cb: function(){ done() }}
      spyOn(errorCb, 'cb').and.callThrough()
      image = draw.image('not_existant.jpg', 100, 100).loaded(loadCb).error(errorCb.cb)
    })

    it('should set the error callback', function() {
      expect(image._error).toBe(errorCb.cb)
    })
    it('executes the error callback', function() {
      expect(errorCb.cb).toHaveBeenCalledWith(jasmine.any(Event))
    })
    it('does not execute the load callback', function() {
      expect(loadCb).not.toHaveBeenCalled()
    })
  })


  describe('x()', function() {
    it('should return the value of x without an argument', function() {
      expect(image.x()).toBe(0)
    })
    it('should set the value of x with the first argument', function() {
      image.x(123)
      var box = image.bbox()
      expect(box.x).toBe(123)
    })
  })

  describe('y()', function() {
    it('should return the value of y without an argument', function() {
      expect(image.y()).toBe(0)
    })
    it('should set the value of y with the first argument', function() {
      image.y(345)
      var box = image.bbox()
      expect(box.y).toBe(345)
    })
  })

  describe('cx()', function() {
    it('should return the value of cx without an argument', function() {
      expect(image.cx()).toBe(50)
    })
    it('should set the value of cx with the first argument', function() {
      image.cx(123)
      var box = image.bbox()
      expect(box.cx).toBe(123)
    })
  })

  describe('cy()', function() {
    it('should return the value of cy without an argument', function() {
      expect(image.cy()).toBe(50)
    })
    it('should set the value of cy with the first argument', function() {
      image.cy(345)
      var box = image.bbox()
      expect(box.cy).toBe(345)
    })
  })

  describe('move()', function() {
    it('should set the x and y position', function() {
      image.move(123,456)
      expect(image.node.getAttribute('x')).toBe('123')
      expect(image.node.getAttribute('y')).toBe('456')
    })
  })

  describe('dx()', function() {
    it('moves the x positon of the element relative to the current position', function() {
      image.move(50,60)
      image.dx(100)
      expect(image.node.getAttribute('x')).toBe('150')
    })
  })

  describe('dy()', function() {
    it('moves the y positon of the element relative to the current position', function() {
      image.move(50,60)
      image.dy(120)
      expect(image.node.getAttribute('y')).toBe('180')
    })
  })

  describe('dmove()', function() {
    it('moves the x and y positon of the element relative to the current position', function() {
      image.move(50,60)
      image.dmove(80, 25)
      expect(image.node.getAttribute('x')).toBe('130')
      expect(image.node.getAttribute('y')).toBe('85')
    })
  })

  describe('center()', function() {
    it('should set the cx and cy position', function() {
      image.center(321,567)
      var box = image.bbox()
      expect(box.cx).toBe(321)
      expect(box.cy).toBe(567)
    })
  })

  describe('width()', function() {
    it('sets the width of the element', function() {
      image.width(789)
      expect(image.node.getAttribute('width')).toBe('789')
    })
    it('gets the width of the element if the argument is null', function() {
      expect(image.width().toString()).toBe(image.node.getAttribute('width'))
    })
  })

  describe('height()', function() {
    it('sets the height of the element', function() {
      image.height(1236)
      expect(image.node.getAttribute('height')).toBe('1236')
    })
    it('gets the height of the element if the argument is null', function() {
      expect(image.height().toString()).toBe(image.node.getAttribute('height'))
    })
  })

  describe('size()', function() {
    it('should define the width and height of the element', function() {
      image.size(987,654)
      expect(image.node.getAttribute('width')).toBe('987')
      expect(image.node.getAttribute('height')).toBe('654')
    })
    it('defines the width and height proportionally with only the width value given', function() {
      var box = image.bbox()
      image.size(500)
      expect(image.width()).toBe(500)
      expect(image.width() / image.height()).toBe(box.width / box.height)
    })
    it('defines the width and height proportionally with only the height value given', function() {
      var box = image.bbox()
      image.size(null, 525)
      expect(image.height()).toBe(525)
      expect(image.width() / image.height()).toBe(box.width / box.height)
    })
  })

  describe('scale()', function() {
    it('should scale the element universally with one argument', function() {
      var box = image.scale(2).rbox()

      expect(box.width).toBe(image.attr('width') * 2)
      expect(box.height).toBe(image.attr('height') * 2)
    })
    it('should scale the element over individual x and y axes with two arguments', function() {
      var box = image.scale(2, 3.5).rbox()

      expect(box.width).toBe(image.attr('width') * 2)
      expect(box.height).toBe(image.attr('height') * 3.5)
    })
  })

  describe('translate()', function() {
    it('should set the translation of an element', function() {
      image.transform({ x: 12, y: 12 })
      expect(image.node.getAttribute('transform')).toBe('matrix(1,0,0,1,12,12)')
    })
  })

})