Anton aa3496af90 fix: alphabetical sort and add flow notation to sort file (#861)
* Removed sort by memberof, when the memberOf is the same of items then sort function doesn't works correctly.

Added Unit Tests
Fixed #838

* Flow.js : added flow notation for sort.js
2017-08-08 14:27:10 -07:00

236 lines
5.6 KiB
JavaScript

var sort = require('../../src/sort'),
path = require('path');
test('sort stream alphanumeric', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var banana = { context: { sortKey: 'c' }, name: 'bananas' };
expect(sort([apples, carrot, banana])).toEqual([apples, carrot, banana]);
expect(sort([carrot, apples, banana])).toEqual([apples, carrot, banana]);
});
test('sort stream with configuration', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
expect(
sort([apples, carrot, bananas], {
toc: ['carrot', 'bananas']
})
).toEqual([carrot, bananas, apples]);
});
test('sort stream with configuration and a section', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
var section = {
name: 'This is the banana type',
description: 'here lies bananas'
};
var sectionMarkdown = {
name: 'This is the banana type',
description: {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'here lies bananas',
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
},
indent: []
}
}
],
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
},
indent: []
}
}
],
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
}
}
},
kind: 'note'
};
expect(
sort([apples, carrot, bananas], {
toc: ['carrot', section, 'bananas']
})
).toEqual([carrot, sectionMarkdown, bananas, apples]);
});
test('sort an already-sorted stream containing a section/description', function() {
// this happens in the 'serve' task
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
var section = {
name: 'This is the banana type',
description: 'here lies bananas'
};
var sectionMarkdown = {
name: 'This is the banana type',
description: {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{
type: 'text',
value: 'here lies bananas',
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
},
indent: []
}
}
],
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
},
indent: []
}
}
],
position: {
start: {
line: 1,
column: 1,
offset: 0
},
end: {
line: 1,
column: 18,
offset: 17
}
}
},
kind: 'note'
};
var config = {
toc: ['carrot', section, 'bananas']
};
var sortOnce = sort([apples, carrot, bananas], config);
var sortTwice = sort(sortOnce, config);
expect(sortTwice).toEqual([carrot, sectionMarkdown, bananas, apples]);
});
test('sort toc with files', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
var snowflake = {
name: 'snowflake',
file: 'test/fixture/snowflake.md'
};
expect(
sort([apples, carrot, bananas], {
toc: [snowflake]
})
).toMatchSnapshot();
});
test('sort toc with files absolute path', function() {
var apples = { context: { sortKey: 'a' }, name: 'apples' };
var carrot = { context: { sortKey: 'b' }, name: 'carrot' };
var bananas = { context: { sortKey: 'c' }, name: 'bananas' };
var snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([apples, carrot, bananas], {
toc: [snowflake]
})
).toMatchSnapshot();
});
test('sort toc with files absolute path', function() {
var apples = {
context: { sortKey: 'a' },
name: 'apples',
memberof: 'classB'
};
var carrot = {
context: { sortKey: 'b' },
name: 'carrot',
memberof: 'classB'
};
var bananas = {
context: { sortKey: 'c' },
name: 'bananas',
memberof: 'classB'
};
var snowflake = {
name: 'snowflake',
file: path.join(__dirname, '../fixture/snowflake.md')
};
expect(
sort([carrot, apples, bananas], {
sortOrder: 'alpha'
})
).toMatchSnapshot();
});