mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-20 07:26:19 +00:00
Vitest: cover dedupeBadges with 6 cases
The badge-deduplication helper was extracted from InfoStandWidgetUserView in the prior commit; it's a pure (badges[]) => badges[] function that keeps slot indices stable by replacing duplicate codes with empty strings. Coverage: - empty input - unique-only passthrough - duplicate-replaced-with-empty - falsy entries (null / undefined / '') normalized to '' - first-occurrence-wins semantics - order sensitivity (same multiset, different order -> different output)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { dedupeBadges } from '../src/api/avatar/dedupeBadges';
|
||||
|
||||
describe('dedupeBadges', () =>
|
||||
{
|
||||
it('returns an empty array for an empty input', () =>
|
||||
{
|
||||
expect(dedupeBadges([])).toEqual([]);
|
||||
});
|
||||
|
||||
it('preserves unique badges in slot order', () =>
|
||||
{
|
||||
expect(dedupeBadges([ 'a', 'b', 'c' ])).toEqual([ 'a', 'b', 'c' ]);
|
||||
});
|
||||
|
||||
it('replaces duplicate slots with empty strings to preserve slot indices', () =>
|
||||
{
|
||||
expect(dedupeBadges([ 'a', 'b', 'a', 'c' ])).toEqual([ 'a', 'b', '', 'c' ]);
|
||||
});
|
||||
|
||||
it('normalizes falsy entries (null, undefined, "") to empty string', () =>
|
||||
{
|
||||
// server sometimes returns null/undefined for unused slots
|
||||
const input = [ 'a', null as unknown as string, '', undefined as unknown as string, 'b' ];
|
||||
|
||||
expect(dedupeBadges(input)).toEqual([ 'a', '', '', '', 'b' ]);
|
||||
});
|
||||
|
||||
it('only keeps the FIRST occurrence of each unique code', () =>
|
||||
{
|
||||
expect(dedupeBadges([ 'a', 'a', 'a' ])).toEqual([ 'a', '', '' ]);
|
||||
});
|
||||
|
||||
it('is order-sensitive: identical multisets but different orderings yield different outputs', () =>
|
||||
{
|
||||
expect(dedupeBadges([ 'a', 'b', 'a' ])).toEqual([ 'a', 'b', '' ]);
|
||||
expect(dedupeBadges([ 'b', 'a', 'a' ])).toEqual([ 'b', 'a', '' ]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user