From e5afd843f44eb37b00d2dc2e749c33be7c873fa1 Mon Sep 17 00:00:00 2001 From: Jan Nanista Date: Tue, 21 Nov 2023 16:50:42 -0800 Subject: [PATCH] chore: Add context to PropertyState --- packages/ua-utils/src/property.ts | 26 +++++++++++------- packages/ua-utils/test/property.test.ts | 35 +++++++++++++++---------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/packages/ua-utils/src/property.ts b/packages/ua-utils/src/property.ts index c51b94c26..c08c735cd 100644 --- a/packages/ua-utils/src/property.ts +++ b/packages/ua-utils/src/property.ts @@ -11,7 +11,7 @@ import assert from "assert" */ export type Property = ( ...context: TContext -) => Promise> +) => Promise> /** * Type encapsulating two states of a configurable property: `Configured` and `Misconfigured` @@ -19,7 +19,9 @@ export type Property = Configured | Misconfigured +export type PropertyState = + | Configured + | Misconfigured /** * Interface for configured state of a configurable property. @@ -27,7 +29,8 @@ export type PropertyState = Configured { +export interface Configured { + context: TContext value: TValue desiredValue?: never configure?: never @@ -39,7 +42,8 @@ export interface Configured { * In misconfigured state, the current value of the property does not match its desired state * and an action needs to be taken to synchronize these two. */ -export interface Misconfigured { +export interface Misconfigured { + context: TContext value: TValue desiredValue: TValue configure: () => TResult | Promise @@ -84,10 +88,10 @@ export const createProperty = assert.deepStrictEqual(value, desiredValue) // The values matched, we return a Configured - return { value } + return { context, value } } catch { // The values did not match, we'll return a Misconfigured - return { value, desiredValue, configure: async () => set(...context, desiredValue) } + return { context, value, desiredValue, configure: async () => set(...context, desiredValue) } } } @@ -97,9 +101,9 @@ export const createProperty = * @param value `PropertyState` * @returns `value is Misconfigured` */ -export const isMisconfigured = ( - value: PropertyState -): value is Misconfigured => "configure" in value && "desiredValue" in value && typeof value.configure === "function" +export const isMisconfigured = ( + value: PropertyState +): value is Misconfigured => "configure" in value && "desiredValue" in value && typeof value.configure === "function" /** * Type assertion utility for narrowing the `PropertyState` type to `Configured` type @@ -107,4 +111,6 @@ export const isMisconfigured = ( * @param value `PropertyState` * @returns `value is Configured` */ -export const isConfigured = (value: PropertyState): value is Configured => !isMisconfigured(value) +export const isConfigured = ( + value: PropertyState +): value is Configured => !isMisconfigured(value) diff --git a/packages/ua-utils/test/property.test.ts b/packages/ua-utils/test/property.test.ts index da4551ba3..c709002d8 100644 --- a/packages/ua-utils/test/property.test.ts +++ b/packages/ua-utils/test/property.test.ts @@ -6,29 +6,29 @@ import { createProperty, isConfigured, isMisconfigured } from "../src/property" describe("property", () => { describe("isMisconfigured", () => { it("should return true if value is Misconfigured", () => { - expect(isMisconfigured({ value: false, desiredValue: true, configure: () => {} })).to.be.true - expect(isMisconfigured({ value: null, desiredValue: null, configure: () => {} })).to.be.true - expect(isMisconfigured({ value: 0, desiredValue: 0, configure: () => {} })).to.be.true + expect(isMisconfigured({ context: [], value: false, desiredValue: true, configure: () => {} })).to.be.true + expect(isMisconfigured({ context: [], value: null, desiredValue: null, configure: () => {} })).to.be.true + expect(isMisconfigured({ context: [], value: 0, desiredValue: 0, configure: () => {} })).to.be.true }) it("should return false if value is Configured", () => { - expect(isMisconfigured({ value: false })).to.be.false - expect(isMisconfigured({ value: true })).to.be.false - expect(isMisconfigured({ value: 1 })).to.be.false + expect(isMisconfigured({ context: [], value: false })).to.be.false + expect(isMisconfigured({ context: [], value: true })).to.be.false + expect(isMisconfigured({ context: [], value: 1 })).to.be.false }) }) describe("isConfigured", () => { it("should return false if value is Configured", () => { - expect(isConfigured({ value: false, desiredValue: true, configure: () => {} })).to.be.false - expect(isConfigured({ value: null, desiredValue: null, configure: () => {} })).to.be.false - expect(isConfigured({ value: 0, desiredValue: 0, configure: () => {} })).to.be.false + expect(isConfigured({ context: [], value: false, desiredValue: true, configure: () => {} })).to.be.false + expect(isConfigured({ context: [], value: null, desiredValue: null, configure: () => {} })).to.be.false + expect(isConfigured({ context: [], value: 0, desiredValue: 0, configure: () => {} })).to.be.false }) it("should return true if value is Misconfigured", () => { - expect(isConfigured({ value: false })).to.be.true - expect(isConfigured({ value: true })).to.be.true - expect(isConfigured({ value: 1 })).to.be.true + expect(isConfigured({ context: [], value: false })).to.be.true + expect(isConfigured({ context: [], value: true })).to.be.true + expect(isConfigured({ context: [], value: 1 })).to.be.true }) }) @@ -43,7 +43,10 @@ describe("property", () => { set: () => {}, }) - expect(isMisconfigured(await property())).to.be.false + const state = await property() + + expect(isMisconfigured(state)).to.be.false + expect(state.context).to.eql([]) }) it("should return Misconfigured if the current and desired don't match", async () => { @@ -56,7 +59,9 @@ describe("property", () => { set: () => {}, }) - expect(isMisconfigured(await property())).to.be.true + const state = await property() + expect(isMisconfigured(state)).to.be.true + expect(state.context).to.eql([]) }) it("should call the setter with desired value when executed", async () => { @@ -74,6 +79,7 @@ describe("property", () => { await state.configure?.() expect(set.calledOnceWith(desiredValue)).to.be.true + expect(state.context).to.eql([]) }) it("should call the setter with context when executed", async () => { @@ -91,6 +97,7 @@ describe("property", () => { await state.configure?.() expect(set.calledOnceWith("context", desiredValue)).to.be.true + expect(state.context).to.eql(["context"]) }) }) })