Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(store): remove defaultsState config property #2118

Merged
merged 1 commit into from
Mar 17, 2024
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
68 changes: 0 additions & 68 deletions packages/store/internals/testing/tests/ngxs.setup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,6 @@ describe('Full testing NGXS States with NgxsTestBed', () => {
});
});

it('should be correct testing lifecycle with NgxsTestBed + defaults', () => {
const { store } = NgxsTestBed.configureTestingStates({
states: [AppState],
ngxsOptions: { defaultsState: { app: { fiz: 'baz' }, foo: 'baz' } }
});

expect(store.snapshot()).toEqual({
app: {
'AppState.ngxsOnInit': true,
'AppState.ngxsAfterBootstrap': true,
count: 2
},
foo: 'baz'
});
});

it('should be correct testing persistence mode', () => {
const { store } = NgxsTestBed.configureTestingStates({
states: [AppState],
ngxsOptions: {
defaultsState: { app: { count: 0 }, foo: 'bar' }
},
before: () => {
ɵInitialState.set({ app: { count: 1 } });
}
});

expect(store.snapshot()).toEqual({
app: { count: 1 },
foo: 'bar'
});
});

it('should be correct testing default disable persistence mode', () => {
const { store } = NgxsTestBed.configureTestingStates({
states: [AppState],
ngxsOptions: {
defaultsState: {
app: {
anyValue: 0
}
}
}
});

expect(store.snapshot()).toEqual({
app: {
'AppState.ngxsOnInit': true,
'AppState.ngxsAfterBootstrap': true,
count: 2
}
});
});

describe('should correct restore state', () => {
class InitialMyState {
public a: number = null!;
Expand All @@ -105,20 +51,6 @@ describe('Full testing NGXS States with NgxsTestBed', () => {
});
});

it('with default state', () => {
const { store } = NgxsTestBed.configureTestingStates({
states: [MyState],
ngxsOptions: {
defaultsState: { defaultValue: 1 }
}
});

expect(store.snapshot()).toEqual({
defaultValue: 1,
myState: { a: null, b: null }
});
});

it('with initial state', () => {
ɵInitialState.set({ defaultValue: 2 });

Expand Down
14 changes: 5 additions & 9 deletions packages/store/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Inject, Injectable, Optional, Signal, computed } from '@angular/core';
import { Observable, of, Subscription, throwError } from 'rxjs';
import { catchError, distinctUntilChanged, map, shareReplay, take } from 'rxjs/operators';
import { ɵINITIAL_STATE_TOKEN, ɵPlainObject } from '@ngxs/store/internals';
import { ɵINITIAL_STATE_TOKEN } from '@ngxs/store/internals';

import { InternalNgxsExecutionStrategy } from './execution/internal-ngxs-execution-strategy';
import { InternalStateOperations } from './internal/state-operations';
Expand Down Expand Up @@ -119,15 +119,11 @@ export class Store {
}

private initStateStream(initialStateValue: any): void {
const value: ɵPlainObject = this._stateStream.value;
const storeIsEmpty: boolean = !value || Object.keys(value).length === 0;
if (storeIsEmpty) {
const defaultStateNotEmpty: boolean = Object.keys(this._config.defaultsState).length > 0;
const storeValues: ɵPlainObject = defaultStateNotEmpty
? { ...this._config.defaultsState, ...initialStateValue }
: initialStateValue;
const value = this._stateStream.value;
const storeIsEmpty = !value || Object.keys(value).length === 0;

this._stateStream.next(storeValues);
if (storeIsEmpty) {
this._stateStream.next(initialStateValue);
}
}
}
9 changes: 1 addition & 8 deletions packages/store/src/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, InjectionToken, Type, inject } from '@angular/core';
import { Observable } from 'rxjs';

import { StateOperator } from '@ngxs/store/operators';
import { ɵPlainObject, ɵSharedSelectorOptions, ɵStateClass } from '@ngxs/store/internals';
import { ɵSharedSelectorOptions, ɵStateClass } from '@ngxs/store/internals';

import { NgxsExecutionStrategy } from './execution/symbols';
import { DispatchOutsideZoneNgxsExecutionStrategy } from './execution/dispatch-outside-zone-ngxs-execution-strategy';
Expand Down Expand Up @@ -84,13 +84,6 @@ export class NgxsConfig {
* (default: null)
*/
executionStrategy: Type<NgxsExecutionStrategy> = DispatchOutsideZoneNgxsExecutionStrategy;
/**
* Defining the default state before module initialization
* This is convenient if we need to create a define our own set of states.
* @deprecated will be removed after v4
* (default: {})
*/
defaultsState: ɵPlainObject = {};
/**
* Defining shared selector options
*/
Expand Down
108 changes: 0 additions & 108 deletions packages/store/tests/default-states.spec.ts

This file was deleted.

Loading