-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update UT + Expose CriteriaAttribute
- Loading branch information
1 parent
b160ce3
commit 77e94e8
Showing
3 changed files
with
54 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |