Skip to content

Commit 0c0bc7f

Browse files
test: add tests for normalizeDeps in versions.ts
Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent 151ea0f commit 0c0bc7f

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

src/versions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ interface DepsChangeSummary {
4848
changed: number;
4949
}
5050

51-
function normalizeDeps(input: unknown): Deps | undefined {
51+
export function normalizeDeps(input: unknown): Deps | undefined {
5252
if (!input) {
5353
return undefined;
5454
}

tests/versions.test.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import * as api from '../src/api';
1515
import * as app from '../src/app';
1616
import * as utils from '../src/utils';
1717
import * as git from '../src/utils/git';
18-
import { bindVersionToPackages, versionCommands } from '../src/versions';
18+
import {
19+
bindVersionToPackages,
20+
normalizeDeps,
21+
versionCommands,
22+
} from '../src/versions';
1923

2024
describe('bindVersionToPackages', () => {
2125
let consoleSpy: ReturnType<typeof spyOn>;
@@ -495,3 +499,60 @@ describe('rollout validation (via versionCommands.update)', () => {
495499
expect(parseRollout('33.7')).toBe(33);
496500
});
497501
});
502+
503+
describe('normalizeDeps', () => {
504+
test('returns undefined for falsy inputs', () => {
505+
expect(normalizeDeps(undefined)).toBeUndefined();
506+
expect(normalizeDeps(null)).toBeUndefined();
507+
expect(normalizeDeps(false)).toBeUndefined();
508+
expect(normalizeDeps('')).toBeUndefined();
509+
});
510+
511+
test('handles string inputs', () => {
512+
// Valid JSON object with string values
513+
expect(normalizeDeps('{"react":"18.2.0","lodash":"4.17.21"}')).toEqual({
514+
react: '18.2.0',
515+
lodash: '4.17.21',
516+
});
517+
518+
// Valid JSON but not an object (array)
519+
expect(normalizeDeps('["react", "lodash"]')).toBeUndefined();
520+
521+
// Valid JSON but non-string values
522+
expect(normalizeDeps('{"react": 18, "lodash": true}')).toBeUndefined();
523+
524+
// Invalid JSON
525+
expect(normalizeDeps('invalid-json')).toBeUndefined();
526+
});
527+
528+
test('handles object inputs', () => {
529+
// Valid object
530+
expect(normalizeDeps({ react: '18.2.0', lodash: '4.17.21' })).toEqual({
531+
react: '18.2.0',
532+
lodash: '4.17.21',
533+
});
534+
535+
// Object with mixed values (ignores non-strings and empty strings)
536+
expect(
537+
normalizeDeps({
538+
react: '18.2.0',
539+
lodash: 4,
540+
empty: '',
541+
bool: true,
542+
obj: {},
543+
}),
544+
).toEqual({
545+
react: '18.2.0',
546+
});
547+
548+
// Empty object after filtering
549+
expect(normalizeDeps({ number: 1, bool: true })).toBeUndefined();
550+
});
551+
552+
test('returns undefined for invalid object types', () => {
553+
// Array
554+
expect(normalizeDeps(['react'])).toBeUndefined();
555+
// Function
556+
expect(normalizeDeps(() => {})).toBeUndefined();
557+
});
558+
});

0 commit comments

Comments
 (0)