|
| 1 | +const { expect } = require('chai'); |
| 2 | +const { at } = require('../../dist/polyfills'); |
| 3 | + |
| 4 | +const defaultArrayMock = { |
| 5 | + 0: 'a', |
| 6 | + 1: 'b', |
| 7 | + 2: 'c', |
| 8 | + 3: 'd', |
| 9 | + length: 4, |
| 10 | + at, |
| 11 | +}; |
| 12 | + |
| 13 | +describe('Array.at', () => { |
| 14 | + it('should handle zero index', () => { |
| 15 | + const obj = { ...defaultArrayMock }; |
| 16 | + expect(obj.at(0)).to.eq('a'); |
| 17 | + expect(obj.at(Number('+0'))).to.eq('a'); |
| 18 | + expect(obj.at(Number('-0'))).to.eq('a'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should handle positive index', () => { |
| 22 | + const obj = { ...defaultArrayMock }; |
| 23 | + expect(obj.at(2)).to.eq('c'); |
| 24 | + expect(obj.at(2.2)).to.eq('c'); |
| 25 | + expect(obj.at(2.8)).to.eq('c'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should handle negative index', () => { |
| 29 | + const obj = { ...defaultArrayMock }; |
| 30 | + expect(obj.at(-2)).to.eq('c'); |
| 31 | + expect(obj.at(-2.2)).to.eq('c'); |
| 32 | + expect(obj.at(-2.8)).to.eq('c'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle positive infinity index', () => { |
| 36 | + const obj = { ...defaultArrayMock }; |
| 37 | + expect(obj.at(Number.POSITIVE_INFINITY)).to.be.undefined; |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle negative infinity index', () => { |
| 41 | + const obj = { ...defaultArrayMock }; |
| 42 | + expect(obj.at(Number.NEGATIVE_INFINITY)).to.be.undefined; |
| 43 | + }); |
| 44 | + |
| 45 | + it('should handle non-numeric index', () => { |
| 46 | + const obj = { ...defaultArrayMock }; |
| 47 | + expect(obj.at('2')).to.eq('c'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should handle NaN index', () => { |
| 51 | + const obj = { ...defaultArrayMock }; |
| 52 | + expect(obj.at(Number.NaN)).to.eq('a'); |
| 53 | + expect(obj.at('invalid')).to.eq('a'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should handle index out of bounds', () => { |
| 57 | + const obj = { ...defaultArrayMock }; |
| 58 | + expect(obj.at(10)).to.be.undefined; |
| 59 | + expect(obj.at(-10)).to.be.undefined; |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle zero length', () => { |
| 63 | + const obj = { ...defaultArrayMock, length: 0 }; |
| 64 | + expect(obj.at(2)).to.be.undefined; |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle negative length', () => { |
| 68 | + const obj = { ...defaultArrayMock, length: -4 }; |
| 69 | + expect(obj.at(2)).to.be.undefined; |
| 70 | + }); |
| 71 | + |
| 72 | + it('should handle non-numeric length', () => { |
| 73 | + const obj = { ...defaultArrayMock, length: 'invalid' }; |
| 74 | + expect(obj.at(2)).to.be.undefined; |
| 75 | + }); |
| 76 | +}); |
0 commit comments