import { AbstractTestCase } from 'internal-test-helpers'; import { runArrayTests, newFixture } from '../helpers/array'; import { removeAt } from '../../lib/mixins/array'; import { get } from 'ember-metal'; class RemoveAtTests extends AbstractTestCase { '@test removeAt([X], 0) => [] + notify'() { let before = newFixture(1); let after = []; let obj = this.newObject(before); let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(removeAt(obj, 0), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.timesCalled('firstObject'), 1, 'should have notified firstObject once' ); this.assert.equal( observer.timesCalled('lastObject'), 1, 'should have notified lastObject once' ); } '@test removeAt([], 200) => OUT_OF_RANGE_EXCEPTION exception'() { let obj = this.newObject([]); this.assert.throws(() => removeAt(obj, 200), Error); } '@test removeAt([A,B], 0) => [B] + notify'() { let before = newFixture(2); let after = [before[1]]; let obj = this.newObject(before); let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(removeAt(obj, 0), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.timesCalled('firstObject'), 1, 'should have notified firstObject once' ); this.assert.equal( observer.validate('lastObject'), false, 'should NOT have notified lastObject' ); } '@test removeAt([A,B], 1) => [A] + notify'() { let before = newFixture(2); let after = [before[0]]; let obj = this.newObject(before); let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(removeAt(obj, 1), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.timesCalled('lastObject'), 1, 'should have notified lastObject once' ); this.assert.equal( observer.validate('firstObject'), false, 'should NOT have notified firstObject once' ); } '@test removeAt([A,B,C], 1) => [A,C] + notify'() { let before = newFixture(3); let after = [before[0], before[2]]; let obj = this.newObject(before); let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(removeAt(obj, 1), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.validate('firstObject'), false, 'should NOT have notified firstObject once' ); this.assert.equal( observer.validate('lastObject'), false, 'should NOT have notified lastObject once' ); } '@test removeAt([A,B,C,D], 1,2) => [A,D] + notify'() { let before = newFixture(4); let after = [before[0], before[3]]; let obj = this.newObject(before); let observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(removeAt(obj, 1, 2), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.validate('firstObject'), false, 'should NOT have notified firstObject once' ); this.assert.equal( observer.validate('lastObject'), false, 'should NOT have notified lastObject once' ); } '@test [A,B,C,D].removeAt(1,2) => [A,D] + notify'() { var obj, before, after, observer; before = newFixture(4); after = [before[0], before[3]]; obj = this.newObject(before); observer = this.newObserver(obj, '[]', '@each', 'length', 'firstObject', 'lastObject'); obj.getProperties('firstObject', 'lastObject'); /* Prime the cache */ this.assert.equal(obj.removeAt(1, 2), obj, 'return self'); this.assert.deepEqual(this.toArray(obj), after, 'post item results'); this.assert.equal(get(obj, 'length'), after.length, 'length'); this.assert.equal(observer.timesCalled('[]'), 1, 'should have notified [] once'); this.assert.equal(observer.timesCalled('@each'), 0, 'should not have notified @each once'); this.assert.equal(observer.timesCalled('length'), 1, 'should have notified length once'); this.assert.equal( observer.validate('firstObject'), false, 'should NOT have notified firstObject once' ); this.assert.equal( observer.validate('lastObject'), false, 'should NOT have notified lastObject once' ); } } runArrayTests('removeAt', RemoveAtTests, 'MutableArray', 'NativeArray', 'ArrayProxy');