Skip to content

Commit

Permalink
Merge pull request #97 from paddls/feature/add_provide_methods
Browse files Browse the repository at this point in the history
feat: add provide method
  • Loading branch information
oscar-guerin authored Nov 4, 2024
2 parents fb76085 + 1bc153c commit 685dba5
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable, Type } from '@angular/core';
import { InjectRepository, NgxRepositoryModule } from '@paddls/ngx-repository';
import { TestBed } from '@angular/core/testing';
import { Firestore, getFirestore } from 'firebase/firestore';
import { initializeApp } from 'firebase/app';
import { FirestoreRepository } from '../../lib/repository/firestore.repository';
import { NgxFirestoreRepositoryModule } from '../../lib/ngx-firestore-repository.module';
import { FIRESTORE_APP } from '../../lib/ngx-firestore-repository.module.di';
import {Injectable, Type} from '@angular/core';
import {InjectRepository, provideNgxRepositoryModule} from '@paddls/ngx-repository';
import {TestBed} from '@angular/core/testing';
import {Firestore, getFirestore} from 'firebase/firestore';
import {initializeApp} from 'firebase/app';
import {FirestoreRepository} from '../../lib/repository/firestore.repository';
import {provideNgxFirestoreRepository} from '../../lib/ngx-firestore-repository.module';
import {FIRESTORE_APP} from '../../lib/ngx-firestore-repository.module.di';

export interface RepositoryContext<T> {
repository: FirestoreRepository<T, string>;
Expand Down Expand Up @@ -35,11 +35,9 @@ export function initializeRepository<T>(bookImpl: Type<T>, providers: any[] = []
}

TestBed.configureTestingModule({
imports: [
NgxRepositoryModule.forRoot(),
NgxFirestoreRepositoryModule.forRoot()
],
providers: [
provideNgxRepositoryModule(),
provideNgxFirestoreRepository(),
BookServiceImpl,
{
provide: FIRESTORE_APP,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {TestBed} from '@angular/core/testing';
import {
NgxFirestoreRepositoryModule,
NgxFirestoreRepositoryModuleConfiguration,
provideNgxFirestoreRepository
} from './ngx-firestore-repository.module';
import {LogExecuteFirestoreRequestEventListener} from './driver/listener/log-execute-firestore-request-event.listener';
import {FIRESTORE_APP} from './ngx-firestore-repository.module.di';
import {FirestoreNormalizer} from './normalizer/firestore.normalizer';


describe('NgxFirestoreRepositoryModule.forRoot', () => {
const config: NgxFirestoreRepositoryModuleConfiguration = {
debug: true
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxFirestoreRepositoryModule.forRoot(config)],
});
});

testFirestoreRepository();
});

describe('provideNgxFirestoreRepositoryModule', () => {

const config: NgxFirestoreRepositoryModuleConfiguration = {
debug: true
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxFirestoreRepositoryModule],
providers: [
provideNgxFirestoreRepository(config),
]
});
});
testFirestoreRepository();
});

function testFirestoreRepository() {
it('should provide FirestoreRepositoryBuilder', () => {
expect(() => TestBed.inject(FIRESTORE_APP)).toBeTruthy();
});

it('should not provide FirestoreNormalizer', () => {
expect(() => TestBed.inject(FirestoreNormalizer)).toThrowError(/No provider for/);
});

it('should provide LogExecuteFirestoreRequestEventListener when debug is true', () => {
expect(TestBed.inject(LogExecuteFirestoreRequestEventListener)).toBeTruthy();
});
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'reflect-metadata';

import { ModuleWithProviders, NgModule, Provider } from '@angular/core';
import { FirestoreRepositoryBuilder } from './repository/firestore-repository-builder.service';
import { FIRESTORE_APP } from './ngx-firestore-repository.module.di';
import { REPOSITORY_BUILDER_TOKEN } from '@paddls/ngx-repository';
import { FirestoreNormalizer } from './normalizer/firestore.normalizer';
import { FirestoreRepositoryDriver } from './driver/firestore-repository-driver.service';
import { FirestoreRequestBuilder } from './request/firestore-request-builder.service';
import { FirestoreCriteriaRequestBuilder } from './request/firestore-criteria-request-builder.service';
import { LogExecuteFirestoreRequestEventListener } from './driver/listener/log-execute-firestore-request-event.listener';
import { Firestore } from 'firebase/firestore';
import {EnvironmentProviders, makeEnvironmentProviders, ModuleWithProviders, NgModule, Provider} from '@angular/core';
import {FirestoreRepositoryBuilder} from './repository/firestore-repository-builder.service';
import {FIRESTORE_APP} from './ngx-firestore-repository.module.di';
import {REPOSITORY_BUILDER_TOKEN} from '@paddls/ngx-repository';
import {FirestoreNormalizer} from './normalizer/firestore.normalizer';
import {FirestoreRepositoryDriver} from './driver/firestore-repository-driver.service';
import {FirestoreRequestBuilder} from './request/firestore-request-builder.service';
import {FirestoreCriteriaRequestBuilder} from './request/firestore-criteria-request-builder.service';
import {LogExecuteFirestoreRequestEventListener} from './driver/listener/log-execute-firestore-request-event.listener';
import {Firestore} from 'firebase/firestore';

const MODULE_PROVIDERS: Provider[] = [
FirestoreRepositoryBuilder,
Expand All @@ -32,6 +32,22 @@ export interface NgxFirestoreRepositoryModuleConfiguration { // @TODO: RMA/TNI :
debug?: boolean;
}

export function provideNgxFirestoreRepository(config: NgxFirestoreRepositoryModuleConfiguration = {debug: false}): EnvironmentProviders {
const providers: Provider[] = [
...MODULE_PROVIDERS,
{
provide: FIRESTORE_APP,
useValue: config.firestore
}
];

if (config.debug) {
providers.push(LogExecuteFirestoreRequestEventListener);
}

return makeEnvironmentProviders(providers);
}

/**
* @ignore
*/
Expand All @@ -42,6 +58,9 @@ export interface NgxFirestoreRepositoryModuleConfiguration { // @TODO: RMA/TNI :
})
export class NgxFirestoreRepositoryModule {

/**
* @deprecated The method should not be used, use provideNgxFirestoreRepository instead
*/
public static forRoot(config: NgxFirestoreRepositoryModuleConfiguration = {debug: false}): ModuleWithProviders<NgxFirestoreRepositoryModule> {
const providers: Provider[] = [
{
Expand Down
4 changes: 3 additions & 1 deletion projects/ngx-firestore-repository/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ export { FirestoreEmptyRepositoryResponse } from './lib/response/firestore-empty
export { FirestoreRepositoryResponse } from './lib/response/firestore-repository.response';

export { FIRESTORE_APP } from './lib/ngx-firestore-repository.module.di';
export { NgxFirestoreRepositoryModule, NgxFirestoreRepositoryModuleConfiguration } from './lib/ngx-firestore-repository.module';
export {
provideNgxFirestoreRepository, NgxFirestoreRepositoryModule, NgxFirestoreRepositoryModuleConfiguration
} from './lib/ngx-firestore-repository.module';
22 changes: 10 additions & 12 deletions projects/ngx-http-repository/src/e2e/http-request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { TestBed } from '@angular/core/testing';
import {TestBed} from '@angular/core/testing';
import {
Column,
Id,
NgxRepositoryModule,
NgxRepositoryService,
Page,
PageResponseProcessor,
PathColumn,
PathParam
PathParam,
provideNgxRepositoryModule
} from '@paddls/ngx-repository';
import { NgxHttpRepositoryModule } from '../lib/ngx-http-repository.module';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { expectHttpRequest, HttpRequestTestContext } from './util/expect-http-request.spec';
import {provideNgxHttpRepositoryModule} from '../lib/ngx-http-repository.module';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {expectHttpRequest, HttpRequestTestContext} from './util/expect-http-request.spec';
import {
HttpBodyFn,
HttpDelete,
Expand All @@ -25,7 +25,7 @@ import {
HttpQueryFn,
HttpQueryParam
} from '../public-api';
import { buildHttpParams } from './util/build-http.params.spec';
import {buildHttpParams} from './util/build-http.params.spec';
import forOwn from 'lodash.forown';

describe('HttpRequestDecorator', () => {
Expand Down Expand Up @@ -193,11 +193,9 @@ describe('HttpRequestDecorator', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxRepositoryModule.forRoot(),
NgxHttpRepositoryModule.forRoot()
],
providers: [
provideNgxRepositoryModule(),
provideNgxHttpRepositoryModule(),
Api
]
});
Expand Down
21 changes: 10 additions & 11 deletions projects/ngx-http-repository/src/e2e/live-resource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { TestBed } from '@angular/core/testing';
import { Column, Id, InjectRepository, NgxRepositoryModule, Page } from '@paddls/ngx-repository';
import { HttpLiveResource, HttpRepository, HttpResource, NgxHttpRepositoryModule } from '../public-api';
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { of } from 'rxjs';
import { take, toArray } from 'rxjs/operators';
import {TestBed} from '@angular/core/testing';
import {Column, Id, InjectRepository, Page, provideNgxRepositoryModule} from '@paddls/ngx-repository';
import {HttpLiveResource, HttpRepository, HttpResource} from '../public-api';
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {of} from 'rxjs';
import {take, toArray} from 'rxjs/operators';
import {provideNgxHttpRepositoryModule} from '../lib/ngx-http-repository.module';

describe('LiveResource', () => {

Expand Down Expand Up @@ -38,11 +39,9 @@ describe('LiveResource', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxRepositoryModule.forRoot(),
NgxHttpRepositoryModule.forRoot()
],
providers: [
provideNgxRepositoryModule(),
provideNgxHttpRepositoryModule(),
BookService
]
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpClient } from '@angular/common/http';
import { Injectable, Type } from '@angular/core';
import { InjectRepository, NgxRepositoryModule } from '@paddls/ngx-repository';
import { HttpRepository } from '../../lib/repository/http.repository';
import { TestBed } from '@angular/core/testing';
import { NgxHttpRepositoryModule } from '../../lib/ngx-http-repository.module';
import {HttpClient} from '@angular/common/http';
import {Injectable, Type} from '@angular/core';
import {InjectRepository, provideNgxRepositoryModule} from '@paddls/ngx-repository';
import {HttpRepository} from '../../lib/repository/http.repository';
import {TestBed} from '@angular/core/testing';
import {provideNgxHttpRepositoryModule} from '../../lib/ngx-http-repository.module';

export interface RepositoryContext<T> {
repository: HttpRepository<T, number>;
Expand All @@ -20,11 +20,9 @@ export function initializeRepository<T>(bookImpl: Type<T>, providers: any[] = []
}

TestBed.configureTestingModule({
imports: [
NgxRepositoryModule.forRoot(),
NgxHttpRepositoryModule.forRoot()
],
providers: [
provideNgxRepositoryModule(),
provideNgxHttpRepositoryModule(),
BookServiceImpl,
...providers
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Injectable, Type } from '@angular/core';
import { InjectRepository, NgxRepositoryModule } from '@paddls/ngx-repository';
import { TestBed } from '@angular/core/testing';
import { HttpClient, HttpParams } from '@angular/common/http';
import { HttpRepository } from '../../lib/repository/http.repository';
import { NgxHttpRepositoryModule, NgxHttpRepositoryModuleConfiguration } from '../../public-api';
import { expectHttpRequest, HttpRequestTestContext } from './expect-http-request.spec';
import {Injectable, Type} from '@angular/core';
import {InjectRepository, provideNgxRepositoryModule} from '@paddls/ngx-repository';
import {TestBed} from '@angular/core/testing';
import {HttpClient, HttpParams} from '@angular/common/http';
import {HttpRepository} from '../../lib/repository/http.repository';
import {NgxHttpRepositoryModuleConfiguration} from '../../public-api';
import {expectHttpRequest, HttpRequestTestContext} from './expect-http-request.spec';
import forOwn from 'lodash.forown';
import {provideNgxHttpRepositoryModule} from '../../lib/ngx-http-repository.module';

export interface HttpTestContext extends HttpRequestTestContext {
entity: Type<any>;
Expand Down Expand Up @@ -54,11 +55,9 @@ export async function httpTest(httpTestContext: HttpTestContext): Promise<void>
}

TestBed.configureTestingModule({
imports: [
NgxRepositoryModule.forRoot(),
NgxHttpRepositoryModule.forRoot(httpTestContext.httpConfiguration || { debug: false })
],
providers: [
provideNgxRepositoryModule(),
provideNgxHttpRepositoryModule(httpTestContext.httpConfiguration || {debug: false}),
...(httpTestContext.providers || []),
...providers
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {TestBed} from '@angular/core/testing';
import {
NgxHttpRepositoryModule,
NgxHttpRepositoryModuleConfiguration,
provideNgxHttpRepositoryModule
} from './ngx-http-repository.module';
import {LogExecuteHttpRequestEventListener} from './driver/listener/log-execute-http-request-event.listener';
import {HTTP_REPOSITORY_CONFIGURATION} from './configuration/http-repository.configuration';
import {HttpRepositoryBuilder} from './repository/http-repository.builder';

describe('NgxHttpRepositoryModule.forRoot', () => {
const config: NgxHttpRepositoryModuleConfiguration = {
debug: true
};

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxHttpRepositoryModule.forRoot(config)]
});
});
testHttpRepository();
});

describe('provideNgxHttpRepositoryModule', () => {
const config: NgxHttpRepositoryModuleConfiguration = {
debug: true
};

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideNgxHttpRepositoryModule(config)
]
});
});
testHttpRepository();
});

function testHttpRepository() {

it('should provide HttpRepositoryBuilder', () => {
expect(() => TestBed.inject(HTTP_REPOSITORY_CONFIGURATION)).toBeTruthy();
});

it('should not provide HttpRepositoryDriver', () => {
expect(() => TestBed.inject(HttpRepositoryBuilder)).toThrowError(/No provider for/);
});

it('should provide LogExecuteHttpRequestEventListener when debug is true', () => {
expect(TestBed.inject(LogExecuteHttpRequestEventListener)).toBeTruthy();
});
}
Loading

0 comments on commit 685dba5

Please sign in to comment.