|
| 1 | +import { calculateShapeAdjustedDimensionsBasedOnStrokeHeight } from './shape-adjusted-dimensions'; |
| 2 | +import { ShapeType } from '@/core/model'; |
| 3 | + |
| 4 | +const functionToTest = calculateShapeAdjustedDimensionsBasedOnStrokeHeight; |
| 5 | + |
| 6 | +describe('calculateShapeAdjustedDimensionsBasedOnStrokeHeight', () => { |
| 7 | + // ----- Rectangle Low ----- |
| 8 | + |
| 9 | + describe('when shapeType is rectangleLow', () => { |
| 10 | + const shapeType: ShapeType = 'rectangleLow'; |
| 11 | + const width = 100; |
| 12 | + const height = 100; |
| 13 | + |
| 14 | + it('should return correct adjusted dimensions for strokeWidth 1px', () => { |
| 15 | + // Arrange |
| 16 | + const strokeWidth = 1; |
| 17 | + |
| 18 | + // Act |
| 19 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 20 | + |
| 21 | + //Assert |
| 22 | + expect(result).toEqual({ |
| 23 | + type: shapeType, |
| 24 | + adjustedX: 0.5, |
| 25 | + adjustedY: 0.5, |
| 26 | + adjustedWidth: 99, |
| 27 | + adjustedHeight: 99, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return correct adjusted dimensions for strokeWidth 2px', () => { |
| 32 | + // Arrange |
| 33 | + const strokeWidth = 2; |
| 34 | + |
| 35 | + // Act |
| 36 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 37 | + |
| 38 | + //Assert |
| 39 | + expect(result).toEqual({ |
| 40 | + type: shapeType, |
| 41 | + adjustedX: 1, |
| 42 | + adjustedY: 1, |
| 43 | + adjustedWidth: 98, |
| 44 | + adjustedHeight: 98, |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return correct adjusted dimensions for strokeWidth 4px', () => { |
| 49 | + // Arrange |
| 50 | + const strokeWidth = 4; |
| 51 | + |
| 52 | + // Act |
| 53 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 54 | + |
| 55 | + //Assert |
| 56 | + expect(result).toEqual({ |
| 57 | + type: shapeType, |
| 58 | + adjustedX: 2, |
| 59 | + adjustedY: 2, |
| 60 | + adjustedWidth: 96, |
| 61 | + adjustedHeight: 96, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return correct adjusted dimensions for strokeWidth 8px', () => { |
| 66 | + // Arrange |
| 67 | + const strokeWidth = 8; |
| 68 | + const width = 100; |
| 69 | + const height = 100; |
| 70 | + |
| 71 | + // Act |
| 72 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 73 | + |
| 74 | + //Assert |
| 75 | + expect(result).toEqual({ |
| 76 | + type: shapeType, |
| 77 | + adjustedX: 4, |
| 78 | + adjustedY: 4, |
| 79 | + adjustedWidth: 92, |
| 80 | + adjustedHeight: 92, |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should return original dimensions for strokeWidth 0px', () => { |
| 85 | + // Arrange |
| 86 | + const strokeWidth = 0; |
| 87 | + |
| 88 | + // Act |
| 89 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 90 | + |
| 91 | + //Assert |
| 92 | + expect(result).toEqual({ |
| 93 | + type: shapeType, |
| 94 | + adjustedX: 0, |
| 95 | + adjustedY: 0, |
| 96 | + adjustedWidth: 100, |
| 97 | + adjustedHeight: 100, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should handle strokeWidth equal to width, returning 0 width', () => { |
| 102 | + // Arrange |
| 103 | + const strokeWidth = 100; |
| 104 | + |
| 105 | + // Act |
| 106 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 107 | + |
| 108 | + //Assert |
| 109 | + expect(result).toEqual({ |
| 110 | + type: shapeType, |
| 111 | + adjustedX: 50, |
| 112 | + adjustedY: 50, |
| 113 | + adjustedWidth: 0, |
| 114 | + adjustedHeight: 0, |
| 115 | + }); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + // ----- Circle Low ----- |
| 120 | + |
| 121 | + describe('when shapeType is circleLow', () => { |
| 122 | + const shapeType: ShapeType = 'circleLow'; |
| 123 | + const width = 100; |
| 124 | + const height = 100; |
| 125 | + |
| 126 | + it('should return correct adjusted dimensions for strokeWidth 1px', () => { |
| 127 | + // Arrange |
| 128 | + const strokeWidth = 1; |
| 129 | + |
| 130 | + // Act |
| 131 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 132 | + |
| 133 | + //Assert |
| 134 | + expect(result).toEqual({ |
| 135 | + type: shapeType, |
| 136 | + centerX: 50, |
| 137 | + centerY: 50, |
| 138 | + adjustedRadius: 49.5, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should return correct adjusted dimensions for strokeWidth 2px', () => { |
| 143 | + // Arrange |
| 144 | + const strokeWidth = 2; |
| 145 | + |
| 146 | + // Act |
| 147 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 148 | + |
| 149 | + //Assert |
| 150 | + expect(result).toEqual({ |
| 151 | + type: shapeType, |
| 152 | + centerX: 50, |
| 153 | + centerY: 50, |
| 154 | + adjustedRadius: 49, |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should return correct adjusted dimensions for strokeWidth 4px', () => { |
| 159 | + // Arrange |
| 160 | + const strokeWidth = 4; |
| 161 | + |
| 162 | + // Act |
| 163 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 164 | + |
| 165 | + //Assert |
| 166 | + expect(result).toEqual({ |
| 167 | + type: shapeType, |
| 168 | + centerX: 50, |
| 169 | + centerY: 50, |
| 170 | + adjustedRadius: 48, |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should return correct adjusted dimensions for strokeWidth 8px', () => { |
| 175 | + // Arrange |
| 176 | + const strokeWidth = 8; |
| 177 | + |
| 178 | + // Act |
| 179 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 180 | + |
| 181 | + //Assert |
| 182 | + expect(result).toEqual({ |
| 183 | + type: shapeType, |
| 184 | + centerX: 50, |
| 185 | + centerY: 50, |
| 186 | + adjustedRadius: 46, |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should return original dimensions for strokeWidth 0px', () => { |
| 191 | + // Arrange |
| 192 | + const strokeWidth = 0; |
| 193 | + |
| 194 | + // Act |
| 195 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 196 | + |
| 197 | + //Assert |
| 198 | + expect(result).toEqual({ |
| 199 | + type: shapeType, |
| 200 | + centerX: 50, |
| 201 | + centerY: 50, |
| 202 | + adjustedRadius: 50, |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + it('should handle strokeWidth equal to width, returning 0 width', () => { |
| 207 | + // Arrange |
| 208 | + const strokeWidth = 100; |
| 209 | + |
| 210 | + // Act |
| 211 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 212 | + |
| 213 | + //Assert |
| 214 | + expect(result).toEqual({ |
| 215 | + type: shapeType, |
| 216 | + centerX: 50, |
| 217 | + centerY: 50, |
| 218 | + adjustedRadius: 0, |
| 219 | + }); |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + // ----- Ellipse Low ----- |
| 224 | + |
| 225 | + describe('when shapeType is ellipseLow', () => { |
| 226 | + const shapeType: ShapeType = 'ellipseLow'; |
| 227 | + const width = 120; |
| 228 | + const height = 80; |
| 229 | + |
| 230 | + it('should return correct adjusted dimensions for strokeWidth 1px', () => { |
| 231 | + // Arrange |
| 232 | + const strokeWidth = 1; |
| 233 | + |
| 234 | + // Act |
| 235 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 236 | + |
| 237 | + //Assert |
| 238 | + expect(result).toEqual({ |
| 239 | + type: shapeType, |
| 240 | + centerX: 60, |
| 241 | + centerY: 40, |
| 242 | + adjustedRadiusX: 59.5, |
| 243 | + adjustedRadiusY: 39.5, |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should return correct adjusted dimensions for strokeWidth 2px', () => { |
| 248 | + // Arrange |
| 249 | + const strokeWidth = 2; |
| 250 | + |
| 251 | + // Act |
| 252 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 253 | + |
| 254 | + //Assert |
| 255 | + expect(result).toEqual({ |
| 256 | + type: shapeType, |
| 257 | + centerX: 60, |
| 258 | + centerY: 40, |
| 259 | + adjustedRadiusX: 59, |
| 260 | + adjustedRadiusY: 39, |
| 261 | + }); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should return correct adjusted dimensions for strokeWidth 4px', () => { |
| 265 | + // Arrange |
| 266 | + const strokeWidth = 4; |
| 267 | + |
| 268 | + // Act |
| 269 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 270 | + |
| 271 | + //Assert |
| 272 | + expect(result).toEqual({ |
| 273 | + type: shapeType, |
| 274 | + centerX: 60, |
| 275 | + centerY: 40, |
| 276 | + adjustedRadiusX: 58, |
| 277 | + adjustedRadiusY: 38, |
| 278 | + }); |
| 279 | + }); |
| 280 | + |
| 281 | + it('should return correct adjusted dimensions for strokeWidth 8px', () => { |
| 282 | + // Arrange |
| 283 | + const strokeWidth = 8; |
| 284 | + |
| 285 | + // Act |
| 286 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 287 | + |
| 288 | + //Assert |
| 289 | + expect(result).toEqual({ |
| 290 | + type: shapeType, |
| 291 | + centerX: 60, |
| 292 | + centerY: 40, |
| 293 | + adjustedRadiusX: 56, |
| 294 | + adjustedRadiusY: 36, |
| 295 | + }); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should return original dimensions for strokeWidth 0px', () => { |
| 299 | + // Arrange |
| 300 | + const strokeWidth = 0; |
| 301 | + |
| 302 | + // Act |
| 303 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 304 | + |
| 305 | + //Assert |
| 306 | + expect(result).toEqual({ |
| 307 | + type: shapeType, |
| 308 | + centerX: 60, |
| 309 | + centerY: 40, |
| 310 | + adjustedRadiusX: 60, |
| 311 | + adjustedRadiusY: 40, |
| 312 | + }); |
| 313 | + }); |
| 314 | + |
| 315 | + it('should handle strokeWidth equal to width, returning 0 width', () => { |
| 316 | + // Arrange |
| 317 | + const strokeWidth = 120; |
| 318 | + |
| 319 | + // Act |
| 320 | + const result = functionToTest(strokeWidth, width, height, shapeType); |
| 321 | + |
| 322 | + //Assert |
| 323 | + expect(result).toEqual({ |
| 324 | + type: shapeType, |
| 325 | + centerX: 60, |
| 326 | + centerY: 40, |
| 327 | + adjustedRadiusX: 0, |
| 328 | + adjustedRadiusY: 0, |
| 329 | + }); |
| 330 | + }); |
| 331 | + }); |
| 332 | + |
| 333 | + // ----- Unsupported Shape Type ----- |
| 334 | + |
| 335 | + describe('when shapeType is not supported', () => { |
| 336 | + const width = 100; |
| 337 | + const height = 100; |
| 338 | + |
| 339 | + it('should throw an error', () => { |
| 340 | + // Arrange |
| 341 | + const shapeType: ShapeType = 'triangle'; |
| 342 | + const strokeWidth = 1; |
| 343 | + |
| 344 | + // Act & Assert |
| 345 | + expect(() => |
| 346 | + functionToTest(strokeWidth, width, height, shapeType) |
| 347 | + ).toThrow(`Unsupported shape type: triangle`); |
| 348 | + }); |
| 349 | + |
| 350 | + it('should throw an error', () => { |
| 351 | + // Arrange |
| 352 | + const shapeType: ShapeType = 'image'; |
| 353 | + const strokeWidth = 8; |
| 354 | + |
| 355 | + // Act & Assert |
| 356 | + expect(() => |
| 357 | + functionToTest(strokeWidth, width, height, shapeType) |
| 358 | + ).toThrow(`Unsupported shape type: image`); |
| 359 | + }); |
| 360 | + }); |
| 361 | +}); |
0 commit comments