From 0c0bc7fcb693c8985eb6dde126031bf03a67f265 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 27 Jun 2026 12:19:57 +0000 Subject: [PATCH] test: add tests for normalizeDeps in versions.ts Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --- src/versions.ts | 2 +- tests/versions.test.ts | 63 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/versions.ts b/src/versions.ts index a450f09..c0c08b8 100644 --- a/src/versions.ts +++ b/src/versions.ts @@ -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; } diff --git a/tests/versions.test.ts b/tests/versions.test.ts index 2208fc4..3fd2e4f 100644 --- a/tests/versions.test.ts +++ b/tests/versions.test.ts @@ -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; @@ -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(); + }); +});