Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ interface DepsChangeSummary {
changed: number;
}

function normalizeDeps(input: unknown): Deps | undefined {
export function normalizeDeps(input: unknown): Deps | undefined {
if (!input) {
return undefined;
}
Expand Down
63 changes: 62 additions & 1 deletion tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import * as api from '../src/api';
import * as app from '../src/app';
import * as utils from '../src/utils';
import * as git from '../src/utils/git';
import { bindVersionToPackages, versionCommands } from '../src/versions';
import {
bindVersionToPackages,
normalizeDeps,
versionCommands,
} from '../src/versions';

describe('bindVersionToPackages', () => {
let consoleSpy: ReturnType<typeof spyOn>;
Expand Down Expand Up @@ -495,3 +499,60 @@ describe('rollout validation (via versionCommands.update)', () => {
expect(parseRollout('33.7')).toBe(33);
});
});

describe('normalizeDeps', () => {
test('returns undefined for falsy inputs', () => {
expect(normalizeDeps(undefined)).toBeUndefined();
expect(normalizeDeps(null)).toBeUndefined();
expect(normalizeDeps(false)).toBeUndefined();
expect(normalizeDeps('')).toBeUndefined();
});

test('handles string inputs', () => {
// Valid JSON object with string values
expect(normalizeDeps('{"react":"18.2.0","lodash":"4.17.21"}')).toEqual({
react: '18.2.0',
lodash: '4.17.21',
});

// Valid JSON but not an object (array)
expect(normalizeDeps('["react", "lodash"]')).toBeUndefined();

// Valid JSON but non-string values
expect(normalizeDeps('{"react": 18, "lodash": true}')).toBeUndefined();

// Invalid JSON
expect(normalizeDeps('invalid-json')).toBeUndefined();
});

test('handles object inputs', () => {
// Valid object
expect(normalizeDeps({ react: '18.2.0', lodash: '4.17.21' })).toEqual({
react: '18.2.0',
lodash: '4.17.21',
});

// Object with mixed values (ignores non-strings and empty strings)
expect(
normalizeDeps({
react: '18.2.0',
lodash: 4,
empty: '',
bool: true,
obj: {},
}),
).toEqual({
react: '18.2.0',
});

// Empty object after filtering
expect(normalizeDeps({ number: 1, bool: true })).toBeUndefined();
});

test('returns undefined for invalid object types', () => {
// Array
expect(normalizeDeps(['react'])).toBeUndefined();
// Function
expect(normalizeDeps(() => {})).toBeUndefined();
});
});