Skip to content

Commit

Permalink
feat(authorizenet): support in-memory backend delegate (#3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 10, 2024
1 parent 9feffdc commit 1fa2825
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import {
STATUS,
} from 'angular-in-memory-web-api';

import { DaffInMemorySingleRouteableBackend } from '@daffodil/driver/in-memory';

import { DAFF_AUTHORIZE_NET_IN_MEMORY_COLLECTION_NAME } from '../collection-name.const';
import { DAFF_IN_MEMORY_AUTHORIZE_NET_PAYMENT_ID } from '../drivers/authorize-net-payment-id';

@Injectable({
providedIn: 'root',
})
export class DaffInMemoryBackendAuthorizenetService implements InMemoryDbService {
export class DaffInMemoryBackendAuthorizenetService implements InMemoryDbService, DaffInMemorySingleRouteableBackend {
readonly collectionName = DAFF_AUTHORIZE_NET_IN_MEMORY_COLLECTION_NAME;

createDb(): any {
return {};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DAFF_AUTHORIZE_NET_IN_MEMORY_COLLECTION_NAME = 'authorizenet';
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
DaffAuthorizeNetDriver,
DaffAuthorizeNetPaymentId,
} from '@daffodil/authorizenet/driver';
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';

import { DAFF_IN_MEMORY_AUTHORIZE_NET_PAYMENT_ID } from './authorize-net-payment-id';
import { DaffInMemoryAuthorizeNetService } from './authorize-net.service';
import { DaffInMemoryBackendAuthorizenetService } from '../backend/authorize-net.service';


@NgModule({
Expand All @@ -31,6 +33,9 @@ export class DaffAuthorizeNetInMemoryDriverModule {
provide: DaffAuthorizeNetPaymentId,
useValue: DAFF_IN_MEMORY_AUTHORIZE_NET_PAYMENT_ID,
},
provideDaffInMemoryBackends(
DaffInMemoryBackendAuthorizenetService,
),
],
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
provideHttpClientTesting,
} from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';

import { DaffAuthorizeNetTokenRequest } from '@daffodil/authorizenet';

import { DaffInMemoryAuthorizeNetService } from './authorize-net.service';

describe('Driver | In Memory | AuthorizeNet | AuthorizeNetService', () => {
let authorizeNetService: DaffInMemoryAuthorizeNetService;
describe('@daffodil/authorizenet/driver/in-memory | AuthorizeNetService', () => {
let service: DaffInMemoryAuthorizeNetService;
let httpMock: HttpTestingController;
const flushedResponse = {
response: 'response',
Expand All @@ -24,21 +25,27 @@ describe('Driver | In Memory | AuthorizeNet | AuthorizeNetService', () => {
imports: [],
providers: [
DaffInMemoryAuthorizeNetService,
{
provide: InMemoryBackendConfig,
useValue: {
apiBase: 'api',
},
},
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});

httpMock = TestBed.inject(HttpTestingController);
authorizeNetService = TestBed.inject(DaffInMemoryAuthorizeNetService);
service = TestBed.inject(DaffInMemoryAuthorizeNetService);
});

afterEach(() => {
httpMock.verify();
});

it('should be created', () => {
expect(authorizeNetService).toBeTruthy();
expect(service).toBeTruthy();
});

describe('generateToken', () => {
Expand All @@ -60,12 +67,12 @@ describe('Driver | In Memory | AuthorizeNet | AuthorizeNetService', () => {
securitycode: '123',
},
};
authorizeNetService.generateToken(paymentTokenRequest).subscribe(response => {
service.generateToken(paymentTokenRequest).subscribe(response => {
expect(response).toEqual(flushedResponse);
done();
});

const req = httpMock.expectOne(`${authorizeNetService.url}/generateToken`);
const req = httpMock.expectOne(`${service['url']}/generateToken`);

expect(req.request.method).toBe('POST');
expect(req.request.body).toEqual(paymentTokenRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffAuthorizeNetTokenRequest } from '@daffodil/authorizenet';
import { DaffAuthorizeNetService } from '@daffodil/authorizenet/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

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

@Injectable({
providedIn: 'root',
})
export class DaffInMemoryAuthorizeNetService implements DaffAuthorizeNetService<DaffAuthorizeNetTokenRequest> {
url = '/api/authorizenet';

constructor(private http: HttpClient) {}
export class DaffInMemoryAuthorizeNetService extends DaffInMemoryDriverBase implements DaffAuthorizeNetService<DaffAuthorizeNetTokenRequest> {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
) {
super(config, DAFF_AUTHORIZE_NET_IN_MEMORY_COLLECTION_NAME);
}

generateToken(paymentTokenRequest: DaffAuthorizeNetTokenRequest): Observable<any> {
return this.http.post<DaffAuthorizeNetTokenRequest>(this.url + '/generateToken', paymentTokenRequest);
Expand Down

0 comments on commit 1fa2825

Please sign in to comment.