Skip to content

Commit 339d44c

Browse files
committed
Async Map Sort Specs
- Fixed testing issues with async map sort spec - Adjusted playground to be a consistent es6 format for promises - Fixed issues in async sort specs where it wasn't assigning the right callback
1 parent f58ea6f commit 339d44c

File tree

4 files changed

+33
-42
lines changed

4 files changed

+33
-42
lines changed

docs/playground.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ const users = [1, 2, 3, 4, 5].map(() => ({
88
}));
99

1010
// Async Sort
11-
await AsyncArray.asyncSort(
11+
const sortedUsers = await AsyncArray.asyncSort(
1212
users,
13-
async ({ email: emailA }, { email: emailB }) => await emailA.localeCompare(emailB)
14-
).then(sortedUsers => {
15-
console.log('Asynchronously sorted users');
16-
console.table(sortedUsers);
17-
});
13+
async ({ email: emailA }, { email: emailB }) =>
14+
await emailA.localeCompare(emailB)
15+
);
16+
17+
console.log('Asynchronously sorted users');
18+
console.table(sortedUsers);

src/async-map-sort.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ const asyncMapSort = (module.exports.asyncMapSort = async function asyncMapSort(
2121
validateIsFunction(mappingFunction);
2222
validateIsFunction(comparisonFunction);
2323

24-
const mappedItems = await asyncMapOverIterable(this, mappingFunction);
25-
26-
return asyncSortIterable(mappedItems, comparisonFunction);
24+
return asyncSortIterable(
25+
await asyncMapOverIterable(this, mappingFunction),
26+
comparisonFunction
27+
);
2728
});
2829

2930
/**

test/async-map-sort.spec.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ context('Async Map Sort', () => {
3131
));
3232
});
3333

34-
describe('Given no comparisonFunction', () => {
35-
// It should map and then sort by unicode
36-
});
37-
3834
describe('Given a synchronous mappingFunction', () => {
3935
let result, mappingFunction, mapSortedArray;
4036

@@ -47,18 +43,16 @@ context('Async Map Sort', () => {
4743
it('Should run each mappingFunction in order', () =>
4844
ranCallbacksInOrder(result));
4945

50-
it('Should map each item in order', async () => {
51-
mapSortedArray.every((item, index) =>
52-
expect(item).to.equal(array[index])
53-
);
54-
});
55-
5646
it('Should resolve to a new array', async () => {
5747
expect(mapSortedArray).to.not.equal(array);
5848
});
5949

6050
it('Should sort by unicode after mapping', async () => {
61-
// should sort by unicode
51+
const expectedResult = mapSortedArray.slice().sort();
52+
53+
expectedResult.every((item, index) =>
54+
expect(mapSortedArray[index]).to.equal(item)
55+
);
6256
});
6357
});
6458

@@ -76,18 +70,16 @@ context('Async Map Sort', () => {
7670
it('Should run each mappingFunction in order', () =>
7771
ranCallbacksInOrder(result));
7872

79-
it('Should map each item in order', async () => {
80-
mapSortedArray.every((item, index) =>
81-
expect(item).to.equal(array[index])
82-
);
83-
});
84-
8573
it('Should resolve to a new array', async () => {
8674
expect(mapSortedArray).to.not.equal(array);
8775
});
8876

8977
it('Should sort by unicode after mapping', async () => {
90-
// should sort by unicode
78+
const expectedResult = mapSortedArray.slice().sort();
79+
80+
expectedResult.every((item, index) =>
81+
expect(mapSortedArray[index]).to.equal(item)
82+
);
9183
});
9284
});
9385

@@ -123,26 +115,23 @@ context('Async Map Sort', () => {
123115
]));
124116
});
125117

126-
describe('Given the optional comparisonFunction parameter', () => {
127-
let result, mappingFunction, mapSortedArray, comparisonFunction;
118+
describe('Given an optional comparisonFunction that uses all arguments', () => {
119+
let result, mappingFunction, comparisonFunction;
128120

129121
beforeEach(async () => {
130122
({ callback: mappingFunction } = getCallback());
131123
({ result, callback: comparisonFunction } = getCallback({
132124
isSort: true
133125
}));
134126

135-
mapSortedArray = await asyncMapSort(
136-
array,
137-
mappingFunction,
138-
comparisonFunction
139-
);
127+
await asyncMapSort(array, mappingFunction, comparisonFunction);
140128
});
141129

142-
it('Should sort by the given comparisonFunction after mapping', async () => {
143-
expect(mapSortedArray);
144-
expect(result);
145-
// returned array should have correct ordering given the comparison
146-
});
130+
it('Should have access to first and second elements', () =>
131+
result.every(({ firstEl, secondEl }) =>
132+
expect(array)
133+
.to.contain(firstEl)
134+
.and.contain(secondEl)
135+
));
147136
});
148137
});

test/async-sort.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ context('Async Sort', () => {
3939
let compareFunc, sortedArray;
4040

4141
beforeEach(async () => {
42-
({ compareFunc } = getCallback({ isSort: true }));
42+
({ callback: compareFunc } = getCallback({ isSort: true }));
4343

4444
sortedArray = await asyncSort(array, compareFunc);
4545
});
@@ -58,7 +58,7 @@ context('Async Sort', () => {
5858
let compareFunc, sortedArray;
5959

6060
beforeEach(async () => {
61-
({ compareFunc } = getCallback({ isSort: true }));
61+
({ callback: compareFunc } = getCallback({ isSort: true }));
6262

6363
sortedArray = await asyncSort(array, compareFunc, array);
6464
});
@@ -92,7 +92,7 @@ context('Async Sort', () => {
9292
let compareFunc, result;
9393

9494
beforeEach(async () => {
95-
({ result, compareFunc } = getCallback({ isSort: true }));
95+
({ result, callback: compareFunc } = getCallback({ isSort: true }));
9696

9797
await asyncSort(array, compareFunc);
9898
});

0 commit comments

Comments
 (0)