Skip to content

Commit

Permalink
feat(cart-store-credit): support in-memory backend delegate (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 10, 2024
1 parent 7be26e5 commit edbb83f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
import { DaffCartTotalTypeEnum } from '@daffodil/cart';
import { DaffInMemoryBackendCartRootService } from '@daffodil/cart/driver/in-memory';
import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
import { DaffCartWithStoreCreditFactory } from '@daffodil/cart-store-credit/testing';
import { DaffCustomerStoreCreditInMemoryBackendService } from '@daffodil/customer-store-credit/driver/in-memory';
import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_CART_STORE_CREDIT_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';


/**
Expand All @@ -20,7 +22,9 @@ import { DaffCustomerStoreCreditInMemoryBackendService } from '@daffodil/custome
@Injectable({
providedIn: 'root',
})
export class DaffCartStoreCreditInMemoryBackendService implements InMemoryDbService {
export class DaffCartStoreCreditInMemoryBackendService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_CART_STORE_CREDIT_IN_MEMORY_COLLECTION_NAME;

constructor(
private customerStoreCredit: DaffCustomerStoreCreditInMemoryBackendService,
private cart: DaffInMemoryBackendCartRootService,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_CART_STORE_CREDIT_IN_MEMORY_COLLECTION_NAME = 'cart-store-credit';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
} from '@angular/core';

import { DaffCartStoreCreditDriver } from '@daffodil/cart-store-credit/driver';
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';

import { DaffCartStoreCreditInMemoryDriver } from './store-credit.service';
import { DaffCartStoreCreditInMemoryBackendService } from '../backend/store-credit.service';

/**
* Provides the {@link DaffCartStoreCreditInMemoryDriver} as the {@link DaffCartStoreCreditDriver}.
Expand All @@ -25,6 +27,7 @@ export class DaffCartStoreCreditInMemoryDriverModule {
provide: DaffCartStoreCreditDriver,
useExisting: DaffCartStoreCreditInMemoryDriver,
},
provideDaffInMemoryBackends(DaffCartStoreCreditInMemoryBackendService),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
Expand All @@ -25,6 +26,12 @@ describe('@daffodil/cart-store-credit/driver/in-memory | DaffCartStoreCreditInMe
imports: [],
providers: [
DaffCartStoreCreditInMemoryDriver,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
Expand All @@ -50,7 +57,7 @@ describe('@daffodil/cart-store-credit/driver/in-memory | DaffCartStoreCreditInMe
let result: Observable<DaffCartWithStoreCredit>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.apply(mockCartWithStoreCredit.id);
});

Expand All @@ -72,7 +79,7 @@ describe('@daffodil/cart-store-credit/driver/in-memory | DaffCartStoreCreditInMe
let result: Observable<DaffCartWithStoreCredit>;

beforeEach(() => {
url = service.url;
url = service['url'];
result = service.remove(mockCartWithStoreCredit.id);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
import { DaffCartStoreCreditDriverInterface } from '@daffodil/cart-store-credit/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

import { DAFF_CART_STORE_CREDIT_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';

/**
* The cart store credit in-memory driver to mock the cart store credit backend service.
Expand All @@ -13,15 +17,13 @@ import { DaffCartStoreCreditDriverInterface } from '@daffodil/cart-store-credit/
@Injectable({
providedIn: 'root',
})
export class DaffCartStoreCreditInMemoryDriver implements DaffCartStoreCreditDriverInterface {
/**
* @docs-private
*/
url = '/api/cart-store-credit';

export class DaffCartStoreCreditInMemoryDriver extends DaffInMemoryDriverBase implements DaffCartStoreCreditDriverInterface {
constructor(
private http: HttpClient,
) {}
config: InMemoryBackendConfig,
) {
super(config, DAFF_CART_STORE_CREDIT_IN_MEMORY_COLLECTION_NAME);
}

apply(cartId: DaffCartWithStoreCredit['id']): Observable<DaffCartWithStoreCredit> {
return this.http.post<DaffCartWithStoreCredit>(`${this.url}/${cartId}`, {});
Expand Down

0 comments on commit edbb83f

Please sign in to comment.