@sneat/space-services 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ const baseConfig = require('../../../eslint.config.js');
2
+ const { sneatLibConfig } = require('../../../eslint.lib.config.js');
3
+
4
+ module.exports = [
5
+ ...baseConfig,
6
+ ...sneatLibConfig(__dirname),
7
+ ];
@@ -0,0 +1,7 @@
1
+ {
2
+ "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
3
+ "dest": "../../../dist/libs/space/services",
4
+ "lib": {
5
+ "entryFile": "src/index.ts"
6
+ }
7
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@sneat/space-services",
3
+ "version": "0.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "peerDependencies": {
8
+ "@angular/common": ">=21.0.0",
9
+ "@angular/core": ">=21.0.0",
10
+ "@ionic/angular": ">=8",
11
+ "firebase": "*",
12
+ "@angular/fire": ">=20"
13
+ },
14
+ "dependencies": {
15
+ "tslib": "2.8.1"
16
+ }
17
+ }
package/project.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "space-services",
3
+ "$schema": "../../../node_modules/nx/schemas/project-schema.json",
4
+ "projectType": "library",
5
+ "sourceRoot": "libs/space/services/src",
6
+ "prefix": "sneat",
7
+ "targets": {
8
+ "build": {
9
+ "executor": "@nx/angular:ng-packagr-lite",
10
+ "outputs": [
11
+ "{workspaceRoot}/dist/libs/space/services"
12
+ ],
13
+ "options": {
14
+ "project": "libs/space/services/ng-package.json",
15
+ "tsConfig": "libs/space/services/tsconfig.lib.json"
16
+ },
17
+ "configurations": {
18
+ "production": {
19
+ "tsConfig": "libs/space/services/tsconfig.lib.prod.json"
20
+ },
21
+ "development": {}
22
+ },
23
+ "defaultConfiguration": "production"
24
+ },
25
+ "test": {
26
+ "executor": "@nx/vitest:test",
27
+ "outputs": [
28
+ "{workspaceRoot}/coverage/libs/space/services"
29
+ ],
30
+ "options": {
31
+ "tsConfig": "libs/space/services/tsconfig.spec.json"
32
+ }
33
+ },
34
+ "lint": {
35
+ "executor": "@nx/eslint:lint"
36
+ }
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './lib/services/space-service.module';
2
+ export * from './lib/services/space-nav.service';
3
+ export * from './lib/services/space.service';
4
+ export * from './lib/services/space-item.service';
5
+ export * from './lib/services/space-context.service';
6
+ export * from './lib/services/space-module.service';
7
+
8
+ export * from './lib/components/with-space-input.directive';
@@ -0,0 +1,41 @@
1
+ import { TestBed } from '@angular/core/testing';
2
+ import { NavController } from '@ionic/angular';
3
+ import { AnalyticsService, ErrorLogger } from '@sneat/core';
4
+ import { SpaceNavService } from '../services/space-nav.service';
5
+
6
+ // Simplified test - complex component testing skipped for now
7
+ describe('WithSpaceInput', () => {
8
+ beforeEach(() => {
9
+ TestBed.configureTestingModule({
10
+ providers: [
11
+ {
12
+ provide: SpaceNavService,
13
+ useValue: {
14
+ navigateToSpace: vi.fn(),
15
+ navigateToSpaces: vi.fn(),
16
+ },
17
+ },
18
+ {
19
+ provide: NavController,
20
+ useValue: {
21
+ navigateRoot: vi.fn().mockResolvedValue(true),
22
+ navigateForward: vi.fn().mockResolvedValue(true),
23
+ },
24
+ },
25
+ {
26
+ provide: AnalyticsService,
27
+ useValue: { logEvent: vi.fn() },
28
+ },
29
+ {
30
+ provide: ErrorLogger,
31
+ useValue: { logError: vi.fn(), logErrorHandler: () => vi.fn() },
32
+ },
33
+ ],
34
+ });
35
+ });
36
+
37
+ it('should have SpaceNavService available', () => {
38
+ const service = TestBed.inject(SpaceNavService);
39
+ expect(service).toBeTruthy();
40
+ });
41
+ });
@@ -0,0 +1,40 @@
1
+ import { computed, Directive, effect, inject, input } from '@angular/core';
2
+ import { ISpaceContext } from '@sneat/space-models';
3
+ import { SneatBaseComponent } from '@sneat/ui';
4
+ import { BehaviorSubject, distinctUntilChanged } from 'rxjs';
5
+ import { SpaceNavService } from '../services/space-nav.service';
6
+
7
+ @Directive()
8
+ export abstract class WithSpaceInput extends SneatBaseComponent {
9
+ protected readonly spaceNavService = inject(SpaceNavService);
10
+
11
+ public readonly $space = input.required<ISpaceContext>();
12
+
13
+ protected readonly $spaceID = computed(() => this.$space().id);
14
+ private readonly spaceIDChanged = new BehaviorSubject<string | undefined>(
15
+ undefined,
16
+ );
17
+ protected spaceID$ = this.spaceIDChanged
18
+ .asObservable()
19
+ .pipe(this.takeUntilDestroyed(), distinctUntilChanged());
20
+
21
+ protected readonly $spaceType = computed(() => this.$space().type);
22
+ protected readonly $spaceRef = computed(() => ({
23
+ id: this.$spaceID(),
24
+ type: this.$spaceType(),
25
+ }));
26
+
27
+ constructor() {
28
+ super();
29
+ effect(() => {
30
+ const spaceID = this.$spaceID();
31
+ if (spaceID !== this.spaceIDChanged.value) {
32
+ this.onSpaceIdChanged(spaceID);
33
+ }
34
+ });
35
+ }
36
+
37
+ protected onSpaceIdChanged(spaceID: string): void {
38
+ this.spaceIDChanged.next(spaceID);
39
+ }
40
+ }
@@ -0,0 +1,16 @@
1
+ import { TestBed } from '@angular/core/testing';
2
+
3
+ import { SpaceContextService } from './space-context.service';
4
+
5
+ describe('SpaceContextService', () => {
6
+ let service: SpaceContextService;
7
+
8
+ beforeEach(() => {
9
+ TestBed.configureTestingModule({});
10
+ service = TestBed.inject(SpaceContextService);
11
+ });
12
+
13
+ it('should be created', () => {
14
+ expect(service).toBeTruthy();
15
+ });
16
+ });
@@ -0,0 +1,45 @@
1
+ import { Injectable } from '@angular/core';
2
+ import { ParamMap } from '@angular/router';
3
+ import { distinctUntilChanged, Observable } from 'rxjs';
4
+ import { map } from 'rxjs/operators';
5
+ import { ISpaceContext } from '@sneat/space-models';
6
+ import { SpaceType } from '@sneat/core';
7
+
8
+ @Injectable({
9
+ providedIn: 'root',
10
+ })
11
+ export class SpaceContextService {
12
+ // public trackUrl(
13
+ // route: ActivatedRoute,
14
+ // paramName: string,
15
+ // ): Observable<ISpaceContext | undefined> {
16
+ // return route.paramMap.pipe(
17
+ // map(params => {
18
+ // const id = params.get('spaceID') || undefined;
19
+ // const spaceContext: ISpaceContext | undefined = id ? { id } : undefined;
20
+ // return spaceContext;
21
+ // }),
22
+ // );
23
+ // }
24
+ }
25
+
26
+ export function trackSpaceIdAndTypeFromRouteParameter(
27
+ paramMap$: Observable<ParamMap>,
28
+ ): Observable<ISpaceContext | undefined> {
29
+ return paramMap$.pipe(
30
+ map((params) => {
31
+ const id = params.get('spaceID'),
32
+ type = params.get('spaceType') as SpaceType;
33
+ // console.log('trackSpaceIdAndTypeFromRouteParameter', params, id, type);
34
+ const spaceContext: ISpaceContext | undefined = id
35
+ ? { id: id, type: type || undefined }
36
+ : undefined;
37
+ // console.log('trackSpaceIdAndTypeFromRouteParameter() => spaceContext:', spaceContext)
38
+ return spaceContext;
39
+ }),
40
+ distinctUntilChanged(
41
+ (previous, current) =>
42
+ previous?.id === current?.id && previous?.type == current?.type,
43
+ ),
44
+ );
45
+ }
@@ -0,0 +1,179 @@
1
+ import { TestBed } from '@angular/core/testing';
2
+ import { Injector } from '@angular/core';
3
+ import { Firestore, collection } from '@angular/fire/firestore';
4
+ import { SneatApiService } from '@sneat/api';
5
+ import { firstValueFrom, of } from 'rxjs';
6
+ import {
7
+ GlobalSpaceItemService,
8
+ ModuleSpaceItemService,
9
+ } from './space-item.service';
10
+
11
+ // Mock collection function
12
+ vi.mock('@angular/fire/firestore', async () => {
13
+ const actual = await vi.importActual('@angular/fire/firestore');
14
+ return {
15
+ ...actual,
16
+ collection: vi.fn(() => ({ id: 'mock-collection' })),
17
+ };
18
+ });
19
+
20
+ describe('GlobalSpaceItemService', () => {
21
+ let service: GlobalSpaceItemService<unknown, unknown>;
22
+ let mockInjector: Injector;
23
+ let mockFirestore: Firestore;
24
+ let mockSneatApiService: SneatApiService;
25
+
26
+ beforeEach(() => {
27
+ mockInjector = TestBed.inject(Injector);
28
+ mockFirestore = {
29
+ type: 'Firestore',
30
+ toJSON: () => ({}),
31
+ } as unknown as Firestore;
32
+ mockSneatApiService = {
33
+ post: vi.fn(),
34
+ delete: vi.fn(),
35
+ } as unknown as SneatApiService;
36
+
37
+ service = new GlobalSpaceItemService(
38
+ mockInjector,
39
+ 'test-collection',
40
+ mockFirestore,
41
+ mockSneatApiService,
42
+ );
43
+ });
44
+
45
+ it('should be created', () => {
46
+ expect(service).toBeTruthy();
47
+ });
48
+
49
+ it('should throw error if collectionName is not provided', () => {
50
+ expect(() => {
51
+ new GlobalSpaceItemService(
52
+ mockInjector,
53
+ '',
54
+ mockFirestore,
55
+ mockSneatApiService,
56
+ );
57
+ }).toThrow('collectionName is required');
58
+ });
59
+
60
+ it('should have correct collection name', () => {
61
+ expect(service.collectionName).toBe('test-collection');
62
+ });
63
+
64
+ it('should delete space item', async () => {
65
+ const mockResponse = { success: true };
66
+ vi.spyOn(mockSneatApiService, 'delete').mockReturnValue(of(mockResponse));
67
+
68
+ const request = { spaceID: 'space1' };
69
+ const response = await firstValueFrom(
70
+ service.deleteSpaceItem('test-endpoint', request),
71
+ );
72
+ expect(response).toEqual(mockResponse);
73
+ expect(mockSneatApiService.delete).toHaveBeenCalledWith(
74
+ 'test-endpoint',
75
+ undefined,
76
+ request,
77
+ );
78
+ });
79
+
80
+ it('should create space item', async () => {
81
+ const mockResponse = {
82
+ id: 'item1',
83
+ dbo: { name: 'Test Item' },
84
+ };
85
+ vi.spyOn(mockSneatApiService, 'post').mockReturnValue(of(mockResponse));
86
+
87
+ const spaceRef = { id: 'space1', type: 'team' as const };
88
+ const request = { spaceID: 'space1', data: { name: 'Test' } };
89
+
90
+ const item = await firstValueFrom(
91
+ service.createSpaceItem('create-endpoint', spaceRef, request),
92
+ );
93
+ expect(item.id).toBe('item1');
94
+ expect(item.space).toEqual(spaceRef);
95
+ expect(item.dbo).toEqual(mockResponse.dbo);
96
+ });
97
+
98
+ it('should throw error if create response is empty', async () => {
99
+ vi.spyOn(mockSneatApiService, 'post').mockReturnValue(of(null as unknown));
100
+
101
+ const spaceRef = { id: 'space1', type: 'team' as const };
102
+ const request = { spaceID: 'space1' };
103
+
104
+ await expect(
105
+ firstValueFrom(
106
+ service.createSpaceItem('create-endpoint', spaceRef, request),
107
+ ),
108
+ ).rejects.toThrow('create team item response is empty');
109
+ });
110
+
111
+ it('should throw error if create response has no ID', async () => {
112
+ const mockResponse = { dbo: { name: 'Test' } };
113
+ vi.spyOn(mockSneatApiService, 'post').mockReturnValue(of(mockResponse));
114
+
115
+ const spaceRef = { id: 'space1', type: 'team' as const };
116
+ const request = { spaceID: 'space1' };
117
+
118
+ await expect(
119
+ firstValueFrom(
120
+ service.createSpaceItem('create-endpoint', spaceRef, request),
121
+ ),
122
+ ).rejects.toThrow('create team item response have no ID');
123
+ });
124
+ });
125
+
126
+ describe('ModuleSpaceItemService', () => {
127
+ let service: ModuleSpaceItemService<unknown, unknown>;
128
+ let mockInjector: Injector;
129
+ let mockFirestore: Firestore;
130
+ let mockSneatApiService: SneatApiService;
131
+
132
+ beforeEach(() => {
133
+ mockInjector = TestBed.inject(Injector);
134
+ mockFirestore = {
135
+ type: 'Firestore',
136
+ toJSON: () => ({}),
137
+ } as unknown as Firestore;
138
+ mockSneatApiService = {
139
+ post: vi.fn(),
140
+ } as unknown as SneatApiService;
141
+
142
+ vi.mocked(collection).mockReturnValue({ id: 'spaces' } as unknown);
143
+
144
+ service = new ModuleSpaceItemService(
145
+ mockInjector,
146
+ 'test-module',
147
+ 'items',
148
+ mockFirestore,
149
+ mockSneatApiService,
150
+ );
151
+ });
152
+
153
+ it('should be created', () => {
154
+ expect(service).toBeTruthy();
155
+ });
156
+
157
+ it('should throw error if moduleID is not provided', () => {
158
+ expect(() => {
159
+ new ModuleSpaceItemService(
160
+ mockInjector,
161
+ '',
162
+ 'items',
163
+ mockFirestore,
164
+ mockSneatApiService,
165
+ );
166
+ }).toThrow('moduleID is required');
167
+ });
168
+
169
+ it('should have correct moduleID', () => {
170
+ expect(service.moduleID).toBe('test-module');
171
+ });
172
+
173
+ it('should throw error when creating collection ref without spaceID', () => {
174
+ expect(() => {
175
+ // Access protected method via unknown cast for testing
176
+ (service as unknown as { collectionRef: (spaceID: string) => unknown }).collectionRef('');
177
+ }).toThrow('spaceID is required');
178
+ });
179
+ });