create tests for BaseArrayClass
MODIFY
www/BaseArrayClass.js
www/BaseArrayClass.js
@@ -324,7 +324,7 @@ BaseArrayClass.prototype.filterAsync = function(iteratee, callback) {
return Promise.resolve([]);
}
}
- (new Promise(function(resolve) {
+ return (new Promise(function(resolve) {
var results = [];
self[ARRAY_FIELD].forEach(function(item) {
iteratee.call(self, item, function(isOk) {
@@ -458,12 +458,7 @@ BaseArrayClass.prototype.insertAt = function(index, value, noNotify) {
if (index < 0) {
throw new Error('index must be over number than 0');
}
- if (index > self[ARRAY_FIELD].length) {
- for (var i = self[ARRAY_FIELD].length; i <= index; i++) {
- self[ARRAY_FIELD][i] = undefined;
- }
- }
- self[ARRAY_FIELD][index] = value;
+ self[ARRAY_FIELD].splice(index, 0, value);
if (noNotify !== true) {
self.trigger('insert_at', index);
}
@@ -605,15 +600,11 @@ BaseArrayClass.prototype.reverse = function() {
* according to the string conversion of each element.
*/
BaseArrayClass.prototype.sort = function(compareFunction) {
- if (typeof compareFunction !== 'function' && typeof compareFunction !== 'function') {
- var error = new Error('iteratee must be a function');
- if (typeof callback === 'function') {
- throw error;
+ if (typeof compareFunction === 'function') {
+ this[ARRAY_FIELD].sort(compareFunction);
} else {
- return Promise.reject(error);
- }
+ this[ARRAY_FIELD].sort();
}
- this[ARRAY_FIELD] = this.sort(compareFunction);
};

MODIFY
www/__test__/BaseArrayClass.spec.js
www/__test__/BaseArrayClass.spec.js
@@ -16,8 +16,10 @@ describe('[BaseArrayClass]', () => {
it('should keep item order', () => {
const initArray = ['A', 'B', 'C'];
const _ = new BaseArrayClass(initArray);
- expect(_.mapSeries(function(item, next) {
+ expect(_.mapSeries((item, next) => {
+ setTimeout(() => {
next(item);
+ }, Math.random(3));
})).resolves.toEqual(initArray);
});
});
@@ -26,8 +28,10 @@ describe('[BaseArrayClass]', () => {
it('should keep item order', () => {
const initArray = ['A', 'B', 'C'];
const _ = new BaseArrayClass(initArray);
- expect(_.mapAsync(function(item, next) {
+ expect(_.mapAsync((item, next) => {
+ setTimeout(() => {
next(item);
+ }, Math.random(3));
})).resolves.toEqual(initArray);
});
});
@@ -36,9 +40,11 @@ describe('[BaseArrayClass]', () => {
it('should work as `mapAsync()` if callback is given', (done) => {
const initArray = ['A', 'B', 'C'];
const _ = new BaseArrayClass(initArray);
- _.map(function(item, next) {
+ _.map((item, next) => {
+ setTimeout(() => {
next(item);
- }, function(results) {
+ }, Math.random(3));
+ }, (results) => {
expect(results).toEqual(initArray);
done();
});
@@ -47,9 +53,219 @@ describe('[BaseArrayClass]', () => {
it('should work as `Array.map()` if callback is omitted', () => {
const initArray = ['A', 'B', 'C'];
const _ = new BaseArrayClass(initArray);
- expect(_.map(function(item) {
+ expect(_.map((item) => {
return item;
})).toEqual(initArray);
});
});
+
+ describe('forEachAsync()', () => {
+ it('should execute `iteratee` 3 times', (done) => {
+ const initArray = ['A', 'B', 'C'];
+ const _ = new BaseArrayClass(initArray);
+ let i = 0;
+ _.forEachAsync((item, next) => {
+ setTimeout(() => {
+ i++;
+ next(item);
+ }, 0);
+ }).then(() => {
+ expect(i).toBe(3);
+ done();
+ });
+ });
+ });
+
+ describe('forEach()', () => {
+ it('should work as `forEach()` if callback is given', (done) => {
+ let i = 0;
+ const initArray = ['A', 'B', 'C'];
+ const _ = new BaseArrayClass(initArray);
+ _.forEach((item, next) => {
+ setTimeout(() => {
+ i++;
+ next();
+ }, Math.random(3));
+ }, () => {
+ expect(i).toBe(3);
+ done();
+ });
+ });
+
+ it('should work as `Array.forEach()` if callback is omitted', () => {
+ let i = 0;
+ const initArray = ['A', 'B', 'C'];
+ const _ = new BaseArrayClass(initArray);
+ _.forEach((item) => {
+ i++
+ })
+ expect(i).toBe(3);
+ });
+ });
+
+ describe('filterAsync()', () => {
+ it('should return [0, 2, 4]', (done) => {
+ const initArray = [0, 1, 2, 3, 4, 5];
+ const _ = new BaseArrayClass(initArray);
+ _.filterAsync((item, next) => {
+ setTimeout(() => {
+ next(item % 2 === 0);
+ }, Math.random(5));
+ }).then((results) => {
+ expect(results).toEqual([0, 2, 4]);
+ done();
+ });
+ });
+ });
+
+ describe('filter()', () => {
+ it('should work as `filter()` if callback is given', (done) => {
+ let i = 0;
+ const initArray = [0, 1, 2, 3, 4, 5];
+ const _ = new BaseArrayClass(initArray);
+ _.filter((item, next) => {
+ setTimeout(() => {
+ next(item % 2 !== 0);
+ }, Math.random(3));
+ }, (results) => {
+ expect(results).toEqual([1, 3, 5]);
+ done();
+ });
+ });
+
+ it('should work as `Array.filter()` if callback is omitted', () => {
+ let i = 0;
+ const initArray = [
+ {condition: true},
+ {condition: false},
+ {condition: true}
+ ];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.filter((item) => {
+ return item.condition === true
+ })).toHaveLength(2);
+ });
+ });
+
+ describe('indexOf()', () => {
+ it('should find the first element position', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.indexOf('World')).toBe(1);
+ });
+
+ it('should find the second element position', () => {
+ const initArray = ['Hello', 'World', 'test', 'World'];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.indexOf('World', 2)).toBe(3);
+ });
+ });
+
+ describe('empty()', () => {
+ it('should delete all elements', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.empty();
+ expect(_.getLength()).toBe(0);
+ });
+
+ });
+
+ describe('push()', () => {
+ it('should add the item to the last', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.push('HelloWorld');
+ expect(_.getAt(3)).toEqual('HelloWorld');
+ });
+ });
+
+ describe('insertAt()', () => {
+ it('should add the item at the specified position', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.insertAt(1, 'Aloha');
+ expect(_.getArray()).toEqual(['Hello', 'Aloha', 'World', 'test']);
+ });
+ });
+
+
+ describe('getArray()', () => {
+ it('should return the same array', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.getArray()).toEqual(['Hello', 'World', 'test']);
+ });
+ });
+
+ describe('getAt()', () => {
+ it('should return the same item at specified position', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.getAt(1)).toEqual('World');
+ });
+ });
+
+ describe('setAt()', () => {
+ it('should replace the item of specified position', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.setAt(0, 'Aloha');
+ expect(_.getAt(0)).toEqual('Aloha');
+ });
+ });
+
+ describe('removeAt()', () => {
+ it('should remove the item of specified position', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.removeAt(1);
+ expect(_.getAt(1)).toEqual('test');
+ expect(_.getLength()).toBe(2);
+ });
+ });
+
+ describe('pop()', () => {
+ it('should return the same item of the last item', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ expect(_.pop()).toEqual('test');
+ expect(_.getLength()).toBe(2);
+ });
+ });
+
+
+ describe('reverse()', () => {
+ it('should reverse the array', () => {
+ const initArray = ['Hello', 'World', 'test'];
+ const _ = new BaseArrayClass(initArray);
+ _.reverse();
+ expect(_.getArray()).toEqual(['test', 'World', 'Hello']);
+ });
+ });
+
+ describe('sort()', () => {
+ it('should return ["A", "B", "C", "a", "b", "c"]', () => {
+ const initArray = ['c', 'C', 'A', 'B', 'b', 'a'];
+ const _ = new BaseArrayClass(initArray);
+ _.sort();
+ expect(_.getArray()).toEqual(['A', 'B', 'C', 'a', 'b', 'c']);
+ });
+
+ it('should return ["a", "A", "b", "B", "c", "C"]', () => {
+ const initArray = ['c', 'C', 'A', 'B', 'b', 'a'];
+ const _ = new BaseArrayClass(initArray);
+ _.sort((a, b) => {
+ let charCodeA = a.charCodeAt(0);
+ let charCodeB = b.charCodeAt(0);
+ let lowerA = charCodeA > 96 ? charCodeA - 32 : charCodeA;
+ let lowerB = charCodeB > 96 ? charCodeB - 32 : charCodeB;
+ return lowerA === lowerB ?
+ charCodeB - charCodeA:
+ lowerA > 64 && lowerB > 64 ? lowerA - lowerB:
+ lowerA > 64 && lowerB < 65 ? -1 : 1;
+ });
+ expect(_.getArray()).toEqual(['a', 'A', 'b', 'B', 'c', 'C']);
+ });
+ });
});

create tests for BaseArrayClass