App-Netdisco
view release on metacpan or search on metacpan
xt/js/portsort.test.js view on Meta::CPAN
// Coverage for share/public/javascripts/portsort.js, which registers the
// "portsort" sort type used by the device Ports tab and several reports.
//
// The cases are ported from xt/html/portsort.html, the QUnit page removed in
// 9b8fbcb7. Read the original out of git history at
// acb77e37:xt/html/portsort.html before adding cases, rather than inventing
// them: it is the specification.
'use strict';
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
// portsort.js takes jQuery from global scope and registers its comparators as a
// side effect of being loaded, so the stub has to exist before the require.
globalThis.jQuery = {
extend: Object.assign,
fn: { dataTableExt: { oSort: {} } },
};
require(path.join(__dirname, '..', '..', 'share', 'public', 'javascripts', 'portsort.js'));
const sortTypes = globalThis.jQuery.fn.dataTableExt.oSort;
function assertSortsTo(unordered, expected, because) {
assert.deepStrictEqual(unordered.slice().sort(sortTypes['portsort-asc']), expected, because);
}
describe('registration', () => {
test('portSort__loaded_against_a_jquery_stub__registers_both_directions', () => {
assert.deepStrictEqual(Object.keys(sortTypes).sort(), ['portsort-asc', 'portsort-desc']);
});
test('portSortDescending__interface_names__reverses_the_ascending_order', () => {
const names = ['GigabitEthernet1/2', 'GigabitEthernet1/10', 'GigabitEthernet1/1'];
assert.deepStrictEqual(
names.slice().sort(sortTypes['portsort-desc']),
names.slice().sort(sortTypes['portsort-asc']).reverse());
});
});
describe('different value types', () => {
test('portSort__a_number_and_a_string__puts_the_number_first', () => {
assertSortsTo(['a', 1], [1, 'a']);
});
test('portSort__a_number_and_its_numeric_string__leaves_the_order_alone', () => {
assertSortsTo(['1', 1], ['1', 1]);
});
test('portSort__zero_padded_numeric_strings_and_numbers__puts_the_padded_strings_first', () => {
assertSortsTo(['02', 3, 2, '01'], ['01', '02', 2, 3]);
});
});
describe('numerics', () => {
test('portSort__numeric_strings_and_numbers__orders_by_value_not_by_type', () => {
assertSortsTo(['10', 9, 2, '1', '4'], ['1', 2, '4', 9, '10']);
});
test('portSort__zero_left_padded_numbers__orders_by_the_padding_then_the_digits', () => {
assertSortsTo(['0001', '002', '001'], ['0001', '001', '002']);
});
test('portSort__zero_left_padded_and_bare_numbers__puts_every_padded_form_first', () => {
assertSortsTo([2, 1, '1', '0001', '002', '02', '001'], ['0001', '001', '002', '02', 1, '1', 2]);
});
test('portSort__decimals_of_different_precision__orders_by_value', () => {
assertSortsTo(['10.0401', 10.022, 10.042, '10.021999'], ['10.021999', 10.022, '10.0401', 10.042]);
});
( run in 1.106 second using v1.01-cache-2.11-cpan-744e820c463 )