From c7f164ee0f28d58cfa35a7bee7e75e5419f179d5 Mon Sep 17 00:00:00 2001 From: griest024 Date: Tue, 14 May 2024 15:29:16 -0400 Subject: [PATCH] fix(core): `selectLoading` returns `true` for not loading state (#2798) --- .../state/src/operation/entity/helpers.ts | 21 ---------------- .../entity/helpers/create-fake-id.spec.ts | 21 ++++++++++++++++ .../entity/helpers/create-fake-id.ts | 9 +++++++ .../state/src/operation/entity/public_api.ts | 2 +- libs/core/state/src/operation/selectors.ts | 10 +++++--- .../entity => states}/helpers.spec.ts | 25 +------------------ libs/core/state/src/states/helpers.ts | 17 +++++++++++++ libs/core/state/src/states/public_api.ts | 1 + 8 files changed, 57 insertions(+), 49 deletions(-) delete mode 100644 libs/core/state/src/operation/entity/helpers.ts create mode 100644 libs/core/state/src/operation/entity/helpers/create-fake-id.spec.ts create mode 100644 libs/core/state/src/operation/entity/helpers/create-fake-id.ts rename libs/core/state/src/{operation/entity => states}/helpers.spec.ts (71%) create mode 100644 libs/core/state/src/states/helpers.ts diff --git a/libs/core/state/src/operation/entity/helpers.ts b/libs/core/state/src/operation/entity/helpers.ts deleted file mode 100644 index 9eb48e090f..0000000000 --- a/libs/core/state/src/operation/entity/helpers.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { DaffState } from '../../states/public_api'; - -/** - * Returns whether the state is one that represents an operation in progress. - */ -export function daffStateIsLoading(daffState: DaffState): boolean { - return daffState === DaffState.Adding - || daffState === DaffState.Mutating - || daffState === DaffState.Resolving - || daffState === DaffState.Deleting; -} - -/** - * Generate a fake ID for use with placeholder entities. - * - * @param key An optional key that can augment the randomly generated ID. - * @returns A string cont - */ -export function daffOperationEntityCreateFakeId(key: string = ''): string { - return `ε-${Date.now()}-${key}`; -} diff --git a/libs/core/state/src/operation/entity/helpers/create-fake-id.spec.ts b/libs/core/state/src/operation/entity/helpers/create-fake-id.spec.ts new file mode 100644 index 0000000000..9701bf5e93 --- /dev/null +++ b/libs/core/state/src/operation/entity/helpers/create-fake-id.spec.ts @@ -0,0 +1,21 @@ +import { daffOperationEntityCreateFakeId } from './create-fake-id'; + +describe('@daffodil/core/state | daffOperationEntityCreateFakeId', () => { + let result: string; + + beforeEach(() => { + result = daffOperationEntityCreateFakeId('test'); + }); + + it('should return a string containing a greek lowercase sigma', () => { + expect(result).toContain('ε'); + }); + + it('should return a string containing the passed key', () => { + expect(result).toContain('test'); + }); + + it('should return distinct strings when called in quick succession', () => { + expect(result).not.toEqual(daffOperationEntityCreateFakeId()); + }); +}); diff --git a/libs/core/state/src/operation/entity/helpers/create-fake-id.ts b/libs/core/state/src/operation/entity/helpers/create-fake-id.ts new file mode 100644 index 0000000000..ccf300b11e --- /dev/null +++ b/libs/core/state/src/operation/entity/helpers/create-fake-id.ts @@ -0,0 +1,9 @@ +/** + * Generate a fake ID for use with placeholder entities. + * + * @param key An optional key that can augment the randomly generated ID. + * @returns A string cont + */ +export function daffOperationEntityCreateFakeId(key: string = ''): string { + return `ε-${Date.now()}-${key}`; +} diff --git a/libs/core/state/src/operation/entity/public_api.ts b/libs/core/state/src/operation/entity/public_api.ts index db61aaa2e7..b339fdc81a 100644 --- a/libs/core/state/src/operation/entity/public_api.ts +++ b/libs/core/state/src/operation/entity/public_api.ts @@ -1,5 +1,5 @@ export * from './adapter'; export * from './selectors'; -export * from './helpers'; +export * from './helpers/create-fake-id'; export { DaffOperationEntity } from './type'; export { DaffOperationEntityState } from './state.type'; diff --git a/libs/core/state/src/operation/selectors.ts b/libs/core/state/src/operation/selectors.ts index 983f5ab66e..debc435852 100644 --- a/libs/core/state/src/operation/selectors.ts +++ b/libs/core/state/src/operation/selectors.ts @@ -5,7 +5,11 @@ import { } from '@ngrx/store'; import { DaffOperationState } from './state'; -import { DaffState } from '../states/public_api'; +import { + DaffState, + daffStateIsLoading, + daffStateIsMutating, +} from '../states/public_api'; /** * Selectors for an operation state. @@ -58,7 +62,7 @@ export function daffOperationStateSelectorFactory < ); const selectLoading = createSelector( selectLoadingState, - loadingState => loadingState !== DaffState.Stable, + loadingState => daffStateIsLoading(loadingState), ); const selectResolving = createSelector( selectLoadingState, @@ -66,7 +70,7 @@ export function daffOperationStateSelectorFactory < ); const selectMutating = createSelector( selectLoadingState, - loadingState => loadingState === DaffState.Mutating, + loadingState => daffStateIsMutating(loadingState), ); const selectErrors = createSelector( selectState, diff --git a/libs/core/state/src/operation/entity/helpers.spec.ts b/libs/core/state/src/states/helpers.spec.ts similarity index 71% rename from libs/core/state/src/operation/entity/helpers.spec.ts rename to libs/core/state/src/states/helpers.spec.ts index f2ea7261ce..f70ed6294f 100644 --- a/libs/core/state/src/operation/entity/helpers.spec.ts +++ b/libs/core/state/src/states/helpers.spec.ts @@ -1,9 +1,6 @@ import { DaffState } from '@daffodil/core/state'; -import { - daffOperationEntityCreateFakeId, - daffStateIsLoading, -} from './helpers'; +import { daffStateIsLoading } from './helpers'; describe('@daffodil/core/state | daffStateIsLoading', () => { let result: boolean; @@ -78,23 +75,3 @@ describe('@daffodil/core/state | daffStateIsLoading', () => { }); }); }); - -describe('@daffodil/core/state | daffOperationEntityCreateFakeId', () => { - let result: string; - - beforeEach(() => { - result = daffOperationEntityCreateFakeId('test'); - }); - - it('should return a string containing a greek lowercase sigma', () => { - expect(result).toContain('ε'); - }); - - it('should return a string containing the passed key', () => { - expect(result).toContain('test'); - }); - - it('should return distinct strings when called in quick succession', () => { - expect(result).not.toEqual(daffOperationEntityCreateFakeId()); - }); -}); diff --git a/libs/core/state/src/states/helpers.ts b/libs/core/state/src/states/helpers.ts new file mode 100644 index 0000000000..10a468c15a --- /dev/null +++ b/libs/core/state/src/states/helpers.ts @@ -0,0 +1,17 @@ +import { DaffState } from './state.enum'; + +/** + * Returns whether the state is one that represents an operation in progress. + */ +export function daffStateIsLoading(daffState: DaffState): boolean { + return daffStateIsMutating(daffState) || daffState === DaffState.Resolving; +} + +/** + * Returns whether the state is one that represents a mutable operation in progress. + */ +export function daffStateIsMutating(daffState: DaffState): boolean { + return daffState === DaffState.Adding + || daffState === DaffState.Updating + || daffState === DaffState.Deleting; +} diff --git a/libs/core/state/src/states/public_api.ts b/libs/core/state/src/states/public_api.ts index 6f13a0d37a..032fe70011 100644 --- a/libs/core/state/src/states/public_api.ts +++ b/libs/core/state/src/states/public_api.ts @@ -2,3 +2,4 @@ export { DaffState } from './state.enum'; export { DaffMutableLoadingState } from './mutable-loading'; export { DaffLoadingState } from './loading'; export { DaffStateable } from './stateable.interface'; +export * from './helpers';