From 1d0814412721b17209660be4c2439344f136d2d9 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 14 Mar 2024 07:54:34 -0400 Subject: [PATCH] allow empty values passed to Usernotes constructor --- src/classes/Usernotes.test.ts | 14 +++++++++++++- src/classes/Usernotes.ts | 7 ++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/classes/Usernotes.test.ts b/src/classes/Usernotes.test.ts index e7e5b74..1de8953 100644 --- a/src/classes/Usernotes.test.ts +++ b/src/classes/Usernotes.test.ts @@ -3,7 +3,19 @@ import {compressBlob, decompressBlob} from '../helpers/usernotes'; import type {RawUsernotes} from '../types/RawUsernotes'; import {Usernotes} from './Usernotes'; -test.todo('constructor'); +test('constructor: accept empty input', t => { + t.assert( + new Usernotes() instanceof Usernotes, + 'passing nothing to Usernotes constructor should return a Usernotes instance', + ); + + t.assert( + new Usernotes('') instanceof Usernotes, + 'passing empty string to Usernotes constructor should return a Usernotes instance', + ); +}); + +test.todo('constructor: most other things'); test.todo('get: most other things'); diff --git a/src/classes/Usernotes.ts b/src/classes/Usernotes.ts index 3fc27eb..542fde6 100644 --- a/src/classes/Usernotes.ts +++ b/src/classes/Usernotes.ts @@ -19,7 +19,12 @@ export class Usernotes { /** A mapping of usernames to notes on the given user. */ private users = new Map(); - constructor (jsonString: string) { + constructor (jsonString?: string) { + // if we have no data to start with, we start fresh + if (!jsonString) { + return; + } + let data = migrateUsernotesToLatestSchema(JSON.parse(jsonString)); const rawUsers = decompressBlob(data.blob);