45 lines
2.2 KiB
JavaScript
45 lines
2.2 KiB
JavaScript
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const test = require('node:test');
|
|
const ts = require('typescript');
|
|
|
|
const appRoot = path.resolve(__dirname, '..');
|
|
const source = fs.readFileSync(path.join(appRoot, 'src/shared/lib/row-number.ts'), 'utf8');
|
|
const compiled = ts.transpileModule(source, {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS },
|
|
}).outputText;
|
|
const moduleValue = { exports: {} };
|
|
vm.runInNewContext(compiled, { exports: moduleValue.exports, module: moduleValue });
|
|
const { getSnRowNumber } = moduleValue.exports;
|
|
|
|
test('non-paged rows start at one and do not depend on total count', () => {
|
|
assert.deepEqual([0, 1, 2].map(rowIndex => getSnRowNumber({total: 50, rowIndex})), [1, 2, 3]);
|
|
assert.equal(getSnRowNumber({total: 0, rowIndex: 0}), 1);
|
|
});
|
|
test('server pages continue numbering and preserve the final partial page', () => {
|
|
assert.equal(getSnRowNumber({total: 23, rowIndex: 0, pageIndex: 0, pageSize: 10}), 1);
|
|
assert.equal(getSnRowNumber({total: 23, rowIndex: 0, pageIndex: 1, pageSize: 10}), 11);
|
|
assert.equal(getSnRowNumber({total: 23, rowIndex: 2, pageIndex: 2, pageSize: 10}), 23);
|
|
});
|
|
test('page-size changes and refreshed result order use the new positions', () => {
|
|
assert.equal(getSnRowNumber({rowIndex: 0, pageIndex: 1, pageSize: 20}), 21);
|
|
assert.equal(getSnRowNumber({rowIndex: 0, pageIndex: 0, pageSize: 20}), 1);
|
|
});
|
|
|
|
function sourceFiles(directory) {
|
|
return fs.readdirSync(directory, {withFileTypes: true}).flatMap(entry => {
|
|
const full = path.join(directory, entry.name);
|
|
return entry.isDirectory() ? sourceFiles(full) : /\.tsx?$/.test(entry.name) ? [full] : [];
|
|
});
|
|
}
|
|
test('SN does not use the shared descending helper or legacy descending row formulas', () => {
|
|
for (const file of [...sourceFiles(path.join(appRoot, 'src')), ...sourceFiles(path.join(appRoot, 'app'))]) {
|
|
const text = fs.readFileSync(file, 'utf8');
|
|
assert.doesNotMatch(text, /getStandardRowNumber/, file);
|
|
assert.doesNotMatch(text, /(?:rowNumber|rowNo|no)\s*:\s*(?:[\w.]+\s*-\s*(?:index|detailIndex)|total\w*\s*-)/, file);
|
|
assert.doesNotMatch(text, /total(?:Elements|Count)?\s*-\s*\(\(?[^;{}\n]*(?:index|sourceIndex)/, file);
|
|
}
|
|
});
|