Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(newsletter, contact) strengthen types and remove generics #2447

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/demo/src/app/newsletter/newsletter.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TestBed,
} from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { DaffContainerModule } from '@daffodil/design/container';
import {
Expand Down Expand Up @@ -52,7 +53,7 @@ describe('NewsletterComponent', () => {
facade.success$.next(false);
fixture.detectChanges();

newsletterElement = fixture.debugElement.nativeElement.querySelector('.demo-newsletter__right');
newsletterElement = fixture.debugElement.query(By.css('.demo-newsletter__right'));
});

it('should render class .demo-newsletter__right', () => {
Expand Down Expand Up @@ -114,7 +115,7 @@ describe('NewsletterComponent', () => {
beforeEach(() => {
facade.loading$.next(false);
facade.success$.next(false);
facade.error$.next({ code: 'code', message: 'message' });
facade.error$.next([{ code: 'code', message: 'message' }]);
fixture.detectChanges();

newsletterElement = fixture.debugElement.nativeElement.querySelector('.demo-newsletter__retry');
Expand Down
8 changes: 4 additions & 4 deletions apps/demo/src/app/newsletter/newsletter.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ export class NewsletterComponent implements OnInit {

ngOnInit() {
this.hasError$ = this.error$.pipe(
map(error => !!error),
map(error => error.length > 0),
);
}

onNewsletterSubmit() {
if (this.email.valid) {
this.newsletterFacade.dispatch(new DaffNewsletterSubscribe<DaffNewsletterSubmission>(this._makeSubmission(this.email.value)));
this.newsletterFacade.dispatch(new DaffNewsletterSubscribe(this._makeSubmission(this.email.value)));
}
}
onNewsletterCancel() {
this.newsletterFacade.dispatch(new DaffNewsletterCancel());
}
onNewsletterRetry() {
this.newsletterFacade.dispatch(new DaffNewsletterRetry<DaffNewsletterSubmission>(this._makeSubmission(this.email.value)));
this.newsletterFacade.dispatch(new DaffNewsletterRetry(this._makeSubmission(this.email.value)));
}

private _makeSubmission(email: string): DaffNewsletterSubmission {
return { email };
return email;
}
}
7 changes: 0 additions & 7 deletions libs/contact/driver/hubspot/ng-package.prod.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { InjectionToken } from '@angular/core';

import { createConfigInjectionToken } from '@daffodil/core';
import { DaffHubspotConfig } from '@daffodil/driver/hubspot';

export const DaffContactConfigToken = new InjectionToken<DaffHubspotConfig>(
'DaffContactConfig',
);
export const {
/**
* The injection token that holds the configuration of the hubspot contact driver.
*/
token: DaffContactHubspotConfig,
provider: provideDaffContactHubspotConfig,
} = createConfigInjectionToken<DaffHubspotConfig>(null, 'DaffContactHubspotConfig');
21 changes: 11 additions & 10 deletions libs/contact/driver/hubspot/src/contact.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,40 @@ import {
} from 'jasmine-marbles';
import { Observable } from 'rxjs';

import { DaffContactDriver } from '@daffodil/contact/driver';
import { HubspotResponse } from '@daffodil/driver/hubspot';

import { DaffContactHubspotService } from './contact.service';
import { DAFF_CONTACT_HUBSPOT_FORMS_TOKEN } from './token/hubspot-forms.token';

describe('DaffContactHubspotService', () => {
let contactService;
const stubHubspotResponse: HubspotResponse = { inlineMessage: '123', errors: []};

describe('@daffodil/contact/driver/hubspot | DaffContactHubspotService', () => {
let service: DaffContactHubspotService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{ provide: DaffContactDriver, useClass: DaffContactHubspotService },
{
provide: DAFF_CONTACT_HUBSPOT_FORMS_TOKEN,
useValue: {
submit: (): Observable<any> => hot('--a', { a: { test: '123' }}),
submit: (): Observable<any> => hot('--a', { a: stubHubspotResponse }),
},
},
],
});

contactService = TestBed.inject(DaffContactDriver);
service = TestBed.inject(DaffContactHubspotService);
});

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

describe('when sending', () => {
it('should return an observable of HubspotResponse', () => {
it('should return an observable of DaffContactResponse', () => {
const payload = { email: '[email protected]' };
const expected = cold('--b', { b: { test: '123' }});
expect(contactService.send(payload)).toBeObservable(expected);
const expected = cold('--b', { b: { message: '123' }});
expect(service.send(payload)).toBeObservable(expected);
});
});
});
19 changes: 12 additions & 7 deletions libs/contact/driver/hubspot/src/contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ import {
Injectable,
} from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { DaffContactUnion } from '@daffodil/contact';
import { DaffContactServiceInterface } from '@daffodil/contact/driver';
import {
DaffContactRequest,
DaffContactResponse,
DaffContactServiceInterface,
} from '@daffodil/contact/driver';
import { DaffHubspotFormsService } from '@daffodil/driver/hubspot';

import { DAFF_CONTACT_HUBSPOT_FORMS_TOKEN } from './token/hubspot-forms.token';

/**
* @inheritdoc
*/
@Injectable()
export class DaffContactHubspotService
implements DaffContactServiceInterface<DaffContactUnion, any> {
@Injectable({
providedIn: 'root',
})
export class DaffContactHubspotService implements DaffContactServiceInterface {
constructor(@Inject(DAFF_CONTACT_HUBSPOT_FORMS_TOKEN) private hubspotService: DaffHubspotFormsService) {}

send(payload: DaffContactUnion): Observable<any> {
return this.hubspotService.submit(payload);
send(payload: DaffContactRequest): Observable<DaffContactResponse> {
return this.hubspotService.submit(payload).pipe(map((r) => ({ message: r.inlineMessage })));
}
}
6 changes: 3 additions & 3 deletions libs/contact/driver/hubspot/src/hubspot-driver.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { DaffContactDriver } from '@daffodil/contact/driver';
import { DaffHubspotConfig } from '@daffodil/driver/hubspot';

import { DaffContactConfigToken } from './config/contact-config.interface';
import { provideDaffContactHubspotConfig } from './config/contact-config.interface';
import { DaffContactHubspotService } from './contact.service';

@NgModule({
Expand All @@ -20,8 +20,8 @@ export class DaffContactHubSpotDriverModule {
return {
ngModule: DaffContactHubSpotDriverModule,
providers: [
{ provide: DaffContactDriver, useClass: DaffContactHubspotService },
{ provide: DaffContactConfigToken, useValue: config },
{ provide: DaffContactDriver, useExisting: DaffContactHubspotService },
provideDaffContactHubspotConfig(config),
],
};
}
Expand Down
14 changes: 9 additions & 5 deletions libs/contact/driver/hubspot/src/token/hubspot-forms.token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ import { Title } from '@angular/platform-browser';
import { Router } from '@angular/router';

import {
DaffHubspotFormsService,
daffHubspotFormsServiceFactory,
DaffHubspotFormsInterface,
} from '@daffodil/driver/hubspot';

import { DaffContactConfigToken } from '../config/contact-config.interface';
import { DaffContactHubspotConfig } from '../config/contact-config.interface';

export const DAFF_CONTACT_HUBSPOT_FORMS_TOKEN = new InjectionToken<DaffHubspotFormsService>('DAFF_CONTACT_HUBSPOT_FORMS_TOKEN',
/**
* The InjectionToken that holds the Hubspot Forms Service
* used by the ContactDriver to send submissions to Hubspot.
*/
export const DAFF_CONTACT_HUBSPOT_FORMS_TOKEN = new InjectionToken<DaffHubspotFormsInterface>('DAFF_CONTACT_HUBSPOT_FORMS_TOKEN',
{
providedIn: 'root', factory: () => daffHubspotFormsServiceFactory(
factory: () => daffHubspotFormsServiceFactory(
inject(HttpClient),
inject(DOCUMENT),
inject(Router),
inject(Title),
inject(DaffContactConfigToken),
inject(DaffContactHubspotConfig),
),
});
7 changes: 0 additions & 7 deletions libs/contact/driver/in-memory/ng-package.prod.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TestBed } from '@angular/core/testing';
import { STATUS } from 'angular-in-memory-web-api';

import { DaffInMemoryBackendContactService } from './contact.service';

describe('@daffodil/contact/driver/in-memory | DaffInMemoryBackendContactService', () => {
let service: DaffInMemoryBackendContactService;
let reqInfoStub;
let result;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DaffInMemoryBackendContactService],
});

service = TestBed.inject(DaffInMemoryBackendContactService);

reqInfoStub = {
req: {},
utils: {
createResponse$: f => f(),
getJsonBody: req => req.body,
},
};
});

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

describe('on intializiaton', () => {
beforeEach(() => {
result = service.createDb();
});

it('should intialize empty on createDb', () => {
expect(result.submissions).toEqual([]);
});
});

it('should validate that its not empty', () => {
reqInfoStub.req.body = undefined;
result = service.post(reqInfoStub);
expect(result.status).toEqual(STATUS.BAD_REQUEST);
expect(result.statusText).toEqual('Payload is undefined');
});

it('should validate that it doesnt already exist', () => {
reqInfoStub.req.body = { email: '[email protected]' };
service.post(reqInfoStub);
result = service.post(reqInfoStub);
expect(result.status).toEqual(STATUS.BAD_REQUEST);
expect(result.statusText).toEqual('Already contains submission');
});

it('should be able to submit a valid form', () => {
reqInfoStub.req.body = { email: '[email protected]' };
result = service.post(reqInfoStub);
expect(result.status).toEqual(STATUS.OK);
expect(result.body).toEqual({ message: 'Success!' });
});
});
54 changes: 54 additions & 0 deletions libs/contact/driver/in-memory/src/backend/contact.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Injectable } from '@angular/core';
import {
InMemoryDbService,
RequestInfo,
STATUS,
} from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

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

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

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

submissions: DaffContactRequest[] = [];

createDb() {
return {
submissions: this.submissions,
};
}

post(reqInfo: RequestInfo): Observable<DaffContactResponse> {
const body = reqInfo.utils.getJsonBody(reqInfo.req);

return reqInfo.utils.createResponse$(() => {
// validate that its not empty
if (body === undefined) {
return {
status: STATUS.BAD_REQUEST,
statusText: 'Payload is undefined',
};
// validate that it doesn't already exist
} else if (this.submissions.includes(body)) {
return {
status: STATUS.BAD_REQUEST,
statusText: 'Already contains submission',
};
} else {
this.submissions.push(body);
return {
status: STATUS.OK,
body: { message: 'Success!' },
};
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { InMemoryBackendConfig } from 'angular-in-memory-web-api';
import { Observable } from 'rxjs';

import { DaffContactUnion } from '@daffodil/contact';
import { DaffContactServiceInterface } from '@daffodil/contact/driver';
import {
DaffContactServiceInterface,
DaffContactResponse,
} from '@daffodil/contact/driver';
import { DaffInMemoryDriverBase } from '@daffodil/driver/in-memory';

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

/**
* @inheritdoc
*/
@Injectable({
providedIn: 'root',
})
export class DaffInMemoryContactService extends DaffInMemoryDriverBase implements DaffContactServiceInterface<DaffContactUnion, DaffContactUnion>{
export class DaffInMemoryContactService extends DaffInMemoryDriverBase implements DaffContactServiceInterface {
constructor(
private http: HttpClient,
config: InMemoryBackendConfig,
) {
super(config, DAFF_CONTACT_IN_MEMORY_COLLECTION_NAME);
}

send(payload: DaffContactUnion): Observable<DaffContactUnion> {
return this.http.post<DaffContactUnion>(this.url, payload);
send(payload) {
return this.http.post<DaffContactResponse>(this.url, payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DaffContactDriver } from '@daffodil/contact/driver';
import { provideDaffInMemoryBackends } from '@daffodil/driver/in-memory';

import { DaffInMemoryContactService } from './contact.service';
import { DaffInMemoryBackendContactService } from './in-memory-backend/contact-in-memory-backend.service';
import { DaffInMemoryBackendContactService } from '../backend/contact.service';

@NgModule({
imports: [CommonModule],
Expand Down
Loading
Loading