Skip to content

Commit 01de167

Browse files
committed
add new store class to better manager store initialization
1 parent ea4dedf commit 01de167

29 files changed

+666
-147
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ package
4848
.yarn/*
4949
!.yarn/releases
5050

51-
.tests
51+
.tests/*
52+
!.tests/snapshots
5253

5354
lib/package.json
5455
.env
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`store.utils getStorageKey should return storage none and empty key if options is not provided 1`] = `
4+
{
5+
"key": "",
6+
"storage": "none",
7+
}
8+
`;
9+
10+
exports[`store.utils getStorageKey should return storage none and empty key if storage is not provided 1`] = `
11+
{
12+
"key": "TestState",
13+
"storage": "none",
14+
}
15+
`;
16+
17+
exports[`store.utils getStorageKey should return the storage key with only app and name if prefix is not provided 1`] = `
18+
{
19+
"key": "testApp | TestState",
20+
"storage": "session",
21+
}
22+
`;
23+
24+
exports[`store.utils getStorageKey should return the storage key with only name if prefix and app are not provided 1`] = `
25+
{
26+
"key": "TestState",
27+
"storage": "local",
28+
}
29+
`;
30+
31+
exports[`store.utils getStorageKey should return the storage key with only prefix and name if app is not provided 1`] = `
32+
{
33+
"key": "testPrefix | TestState",
34+
"storage": "local",
35+
}
36+
`;
37+
38+
exports[`store.utils getStorageKey should return the storage key with prefix, app, and name 1`] = `
39+
{
40+
"key": "testPrefix | testApp | TestState",
41+
"storage": "local",
42+
}
43+
`;
44+
45+
exports[`store.utils mergeDeep should handle merging when the first argument is undefined 1`] = `
46+
{
47+
"a": 1,
48+
"b": 2,
49+
}
50+
`;
51+
52+
exports[`store.utils mergeDeep should handle merging when the second argument is undefined 1`] = `undefined`;
53+
54+
exports[`store.utils mergeDeep should handle merging with nested null and undefined values 1`] = `
55+
{
56+
"a": {
57+
"b": 1,
58+
"c": 2,
59+
"d": undefined,
60+
},
61+
}
62+
`;
63+
64+
exports[`store.utils mergeDeep should handle null and undefined values gracefully 1`] = `
65+
{
66+
"a": 1,
67+
"b": {
68+
"c": 2,
69+
},
70+
"d": undefined,
71+
}
72+
`;
73+
74+
exports[`store.utils mergeDeep should merge arrays by extending the first array if extendArrays is true 1`] = `
75+
{
76+
"a": [
77+
1,
78+
2,
79+
3,
80+
4,
81+
],
82+
}
83+
`;
84+
85+
exports[`store.utils mergeDeep should merge arrays by replacing the first array if extendArrays is false 1`] = `
86+
{
87+
"a": [
88+
3,
89+
4,
90+
],
91+
}
92+
`;
93+
94+
exports[`store.utils mergeDeep should merge two objects deeply 1`] = `
95+
{
96+
"a": 1,
97+
"b": {
98+
"c": 4,
99+
"d": 3,
100+
"e": 5,
101+
},
102+
"f": 6,
103+
}
104+
`;
105+
106+
exports[`store.utils mergeDeep should override values in the first object with values from the second object 1`] = `
107+
{
108+
"a": 1,
109+
"b": 3,
110+
"c": 4,
111+
}
112+
`;
113+
114+
exports[`store.utils uniqueBy should handle an array with no duplicates 1`] = `
115+
[
116+
{
117+
"id": 1,
118+
"name": "a",
119+
},
120+
]
121+
`;
122+
123+
exports[`store.utils uniqueBy should return an array with unique objects based on a function 1`] = `
124+
[
125+
{
126+
"id": 1,
127+
"name": "a",
128+
},
129+
{
130+
"id": 2,
131+
"name": "b",
132+
},
133+
]
134+
`;
135+
136+
exports[`store.utils uniqueBy should return an array with unique objects based on a property 1`] = `
137+
[
138+
{
139+
"id": 1,
140+
"name": "a",
141+
},
142+
]
143+
`;

demo/src/app/app.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AsyncPipe, JsonPipe } from '@angular/common';
2-
import { Component, OnInit, inject } from '@angular/core';
3-
import { StoreFacade } from './app.facade';
2+
import { Component, OnInit } from '@angular/core';
3+
import { AppStore } from './app.store';
44
import { PartialComponent } from './partial/partial.component';
55

66
@Component({
@@ -12,7 +12,7 @@ import { PartialComponent } from './partial/partial.component';
1212
})
1313
export class AppComponent implements OnInit {
1414
title = 'ngrx-manager';
15-
store = inject(StoreFacade);
15+
store = AppStore.facade;
1616

1717
stateValue = this.store.select('App');
1818
stateObservable = this.store.select('App', true);

demo/src/app/app.facade.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

demo/src/app/app.models.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
export interface AppState {
3+
set: boolean;
4+
extend: boolean;
5+
nestedValue: {
6+
name: string;
7+
age: number;
8+
address: {
9+
address: string;
10+
city: string;
11+
}
12+
arr: (string | number)[]
13+
}
14+
}
15+
16+
export interface SharedState {
17+
useThis: boolean;
18+
useThat: boolean;
19+
}

demo/src/app/app.providers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
22
import { importProvidersFrom } from '@angular/core';
33
import { BrowserModule } from '@angular/platform-browser';
4-
import { provideStoreForRoot } from '@smoosee/ngrx-manager';
5-
import { AppStoreOptions, AppStoreStates } from './app.store';
4+
import { AppStore } from './app.store';
65

76

87

98
export const APP_PROVIDERS = [
109
importProvidersFrom(BrowserModule),
11-
provideStoreForRoot(AppStoreOptions, AppStoreStates),
10+
AppStore.provideForRoot(),
1211
provideHttpClient(withInterceptorsFromDi()),
1312
]

demo/src/app/app.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export type DispatchObject = {
1414

1515
@Injectable({ providedIn: 'root' })
1616
export class AppService {
17-
http = inject(HttpClient);
17+
private http = inject(HttpClient);
1818

1919
appLog(args: LogObject) {
2020
console.log('###', 'appLog', args);
2121
return args;
2222
}
2323

24-
appDispatch(type: 'app' | 'shared') {
24+
appDispatch(type?: 'app' | 'shared') {
2525
return this.http.get<DispatchObject>(`assets/${type}_dispatch.json`);
2626
}
2727

demo/src/app/app.store.ts

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,22 @@
1-
import { StoreAction, StoreOptions, StoreState } from "@smoosee/ngrx-manager";
1+
import { Store, StoreAction, StoreState } from "@smoosee/ngrx-manager";
2+
import { AppState, SharedState } from "./app.models";
23
import { AppService } from "./app.service";
34

4-
export const AppStoreOptions: StoreOptions = {
5-
app: 'app',
6-
prefix: '',
7-
storage: 'local',
8-
flags: {
9-
onSet: 'replace',
10-
onDispatch: 'extend',
11-
}
12-
};
135

14-
interface AppState {
15-
set: boolean;
16-
extend: boolean;
17-
nestedValue: {
18-
name: string;
19-
age: number;
20-
address: {
21-
address: string;
22-
city: string;
6+
export const AppStore = new Store(
7+
{
8+
app: 'app',
9+
prefix: '',
10+
storage: 'local',
11+
flags: {
12+
onSet: 'replace',
13+
onDispatch: 'extend',
2314
}
24-
arr: (string | number)[]
25-
}
26-
}
27-
interface SharedState {
28-
useThis: boolean;
29-
useThat: boolean;
30-
}
31-
32-
export const AppStoreStates = [
15+
}, [
3316
new StoreState({
3417
name: 'App',
35-
fallback: ['Shared'],
3618
initial: <AppState>{},
3719
service: AppService,
38-
actions: [
39-
new StoreAction({
40-
name: 'APP_DEPRECATED',
41-
deprecated: true,
42-
fallback: ['Shared::SHARED_DISPATCH'],
43-
})
44-
]
4520
}),
4621
new StoreState({
4722
name: 'Shared',
@@ -54,4 +29,4 @@ export const AppStoreStates = [
5429
})
5530
]
5631
})
57-
];
32+
]);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { StoreFacade } from '../app.facade';
2+
import { AppStore } from '../app.store';
33

44
@Component({
55
selector: 'app-partial',
66
template: ``,
77
standalone: true
88
})
99
export class PartialComponent implements OnInit {
10-
constructor(private store: StoreFacade) { }
10+
store = AppStore.facade;
1111

1212
ngOnInit() {
1313
this.store.set('App', { test: 123 } as any);
1414
}
15-
}
15+
}

jest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const config: Config = {
152152
// slowTestThreshold: 5,
153153

154154
// A custom snapshot paths resolver
155-
// snapshotResolver: '<rootDir>/jest.resolver.ts',
155+
snapshotResolver: '<rootDir>/jest.resolver.ts',
156156

157157
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
158158
snapshotSerializers: [

0 commit comments

Comments
 (0)