Skip to content

Commit

Permalink
Update UT + Expose CriteriaAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
aldipermanaetikaputra committed Apr 8, 2023
1 parent b160ce3 commit 77e94e8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 26 deletions.
70 changes: 47 additions & 23 deletions src/Topsis.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
import Topsis from './Topsis.js';

test('Rank 4 alternatives with 6 criteria', () => {
const topsis = new Topsis([
{ weight: 4, type: 'cost' },
{ weight: 5, type: 'benefit' },
{ weight: 4, type: 'benefit' },
{ weight: 3, type: 'benefit' },
{ weight: 3, type: 'benefit' },
{ weight: 2, type: 'benefit' },
]);

expect(
topsis.rank(
[
[3500, 70, 10, 80, 3000, 36],
[4500, 90, 10, 60, 2500, 48],
[4000, 80, 9, 90, 2000, 48],
[4000, 70, 8, 50, 1500, 60],
],
true
)
).toEqual([0, 2, 1, 3]);
import Topsis, { CriteriaAttribute } from './Topsis.js';

describe('Topsis', () => {
const criteria: CriteriaAttribute[] = [
{ name: 'Cost', weight: 0.3, type: 'cost' },
{ name: 'Time', weight: 0.2, type: 'benefit' },
{ name: 'Quality', weight: 0.5, type: 'benefit' },
];

const matrix = [
[100, 10, 0.9],
[200, 15, 0.85],
[150, 12, 0.95],
[170, 13, 0.91],
];

test('should throw TypeError when matrix argument is not an array', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
expect(() => Topsis.rank(criteria, 123 as unknown as any)).toThrow(TypeError);
});

test('should throw TypeError when matrix column length is not equal to criteria length', () => {
const invalidMatrix = [
[100, 10, 0.9],
[200, 15],
[150, 12, 0.95],
[170, 13, 0.91],
];
expect(() => Topsis.rank(criteria, invalidMatrix)).toThrow(TypeError);
});

test('should return empty array when matrix is empty', () => {
expect(Topsis.rank(criteria, [])).toEqual([]);
});

test('should rank alternatives correctly', () => {
const ranked = Topsis.rank(criteria, matrix, true);
expect(ranked).toEqual([0, 2, 3, 1]);
});

test('should return null when no alternatives are given', () => {
expect(Topsis.best(criteria, [])).toBeNull();
});

test('should return the index of the best alternative', () => {
expect(Topsis.best(criteria, matrix)).toBe(0);
});
});
3 changes: 2 additions & 1 deletion src/Topsis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class Topsis {
}
}

type CriteriaAttribute = {
export type CriteriaAttribute = {
name?: string;
weight: number;
type?: 'benefit' | 'cost';
};
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Topsis from './Topsis.js';
import Topsis, { CriteriaAttribute } from './Topsis.js';

export const rank = Topsis.rank.bind(Topsis);
export const best = Topsis.best.bind(Topsis);
export { Topsis };
export { Topsis, CriteriaAttribute };

export default Topsis;
module.exports = Topsis;

0 comments on commit 77e94e8

Please sign in to comment.