Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bill-7 committed Aug 16, 2021
1 parent 9948ee9 commit b0bad44
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 93 deletions.
42 changes: 22 additions & 20 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<div class="pt-4 px-3">
<app-logo></app-logo>
</div>
<div class="background mt-4">
<div class="unblur pt-2">
<div class="container">
<div class="mt-3 mb-2 pb-4 row justify-content-md-center">
<div class="col-lg-4 pb-3">
<div class="blue-header card-header">
<i class="lni lni-wallet"></i>
<h4 class="pl-3 d-inline">Make Transfer</h4>
<div class="app">
<div class="pt-4 px-3">
<app-logo></app-logo>
</div>
<div class="background mt-4">
<div class="unblur pt-2">
<div class="container">
<div class="mt-3 mb-2 pb-4 row justify-content-center">
<div class="col-lg-4 pb-3">
<div class="blue-header card-header">
<i class="lni lni-wallet"></i>
<h4 class="pl-3 d-inline">Make Transfer</h4>
</div>
<app-transaction-form (newTransfer)="recieveTransfer($event)"></app-transaction-form>
</div>
<app-transaction-form (newTransfer)="recieveTransfer($event)"></app-transaction-form>
</div>
<div class="col-lg-8">
<div class="blue-header card-header">
<i class="lni lni-list"></i>
<h4 class="pl-3 d-inline">Transactions List</h4>
<div class="col-lg-8">
<div class="blue-header card-header">
<i class="lni lni-list"></i>
<h4 class="pl-3 d-inline">Transactions List</h4>
</div>
<app-transaction-list [newTransaction]="transaction"></app-transaction-list>
</div>
<app-transaction-list [newTransaction]="transaction"></app-transaction-list>
</div>
</div>
</div>
</div>
</div>
<app-footer></app-footer>
<app-footer></app-footer>
</div>
13 changes: 0 additions & 13 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,4 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});

it(`should have as title 'backbase-app'`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('backbase-app');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('.content span').textContent).toContain('backbase-app app is running!');
});
});
20 changes: 1 addition & 19 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,6 @@ export class AppComponent {
transaction!: Transaction

recieveTransfer(event: Transfer) {
this.transaction = {
id: "",
categoryCode: "#d9bcf5",
dates: {
valueDate: Date.now()
},
transaction: {
amountCurrency: {
amount: event.amount,
currencyCode: "EUR"
},
type: "New Transfer",
creditDebitIndicator: "DBIT"
},
merchant: {
name: event.account,
accountNumber: ""
}
}
this.transaction = new Transaction(event.amount, "New Transfer", event.account)
}
}
21 changes: 16 additions & 5 deletions src/app/model.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
export interface Transaction {
id: string | null,
categoryCode: string,
export class Transaction {
id!: string;
categoryCode: string;
dates: {
valueDate: string | number
},
};
transaction: {
amountCurrency: {
amount: number,
currencyCode: string
},
type: string,
creditDebitIndicator: string
},
};
merchant: {
name: string,
accountNumber: string
}

constructor(tAmount?: number, tType?: string, merchantName?: string, date?: number) {
this.categoryCode = "#d9bcf5"
this.dates = { valueDate: date ? date : 1234 }
this.transaction = {
amountCurrency: { amount: tAmount || 0, currencyCode: "EUR" },
type: tType || "New Transfer",
creditDebitIndicator: "DBIT"
}
this.merchant = { name: merchantName || "Merchant", accountNumber: "" }
}
}

export interface Transfer {
Expand Down
21 changes: 11 additions & 10 deletions src/app/transaction-form/transaction-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
<div class="mb-3">
<label for="toAcc" class="form-label">To Account</label>
<input id="toAcc" type="text" class="form-control" formControlName="toAcc" placeholder="type an account name"
[ngClass]="{ 'is-invalid': isControlInvalid(cs.toAcc) }">
<label class="mt-1 error" *ngIf="isControlInvalid(cs.toAcc)">this field is required</label>
[ngClass]="{ 'is-invalid': isControlInvalid(transactionForm.controls.toAcc) }">
<label class="mt-1 error" *ngIf="isControlInvalid(transactionForm.controls.toAcc)">this field is
required</label>
</div>
<label for="amount" class="form-label">Amount</label>
<div class="mb-3">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"></span>
</div>
<input [ngClass]="{ 'is-invalid': isControlInvalid(cs.amount) }" id="amount" type="number"
class="form-control" formControlName="amount" placeholder="specify an amount">
<input [ngClass]="{ 'is-invalid': isControlInvalid(transactionForm.controls.amount) }" id="amount"
type="number" class="form-control" formControlName="amount" placeholder="specify an amount">
</div>
<div class="mt-1" *ngIf="isControlInvalid(cs.amount)">
<label class="error" *ngIf="cs.amount.errors?.required">this field is required</label>
<label class="error" *ngIf="cs.amount.errors?.max">there is not enough balance</label>
<label class="error" *ngIf="cs.amount.errors?.min">negative values are invalid</label>
<div class="mt-1" *ngIf="isControlInvalid(transactionForm.controls.amount)">
<label class="error" *ngIf="transactionForm.controls.amount.errors?.required">this field is required</label>
<label class="error" *ngIf="transactionForm.controls.amount.errors?.max">there is not enough balance</label>
<label class="error" *ngIf="transactionForm.controls.amount.errors?.min">negative values are invalid</label>
</div>
</div>
<app-submit-button class="float-right" (submitForm)="onSubmit(modal)"></app-submit-button>
Expand All @@ -41,9 +42,9 @@ <h4 class="modal-title">Review Transfer</h4>
<div class="modal-body">
<p class="font-italic">Are you ready to send out this transfer?</p>
<p class="font-weight-bold">It will be sent to account:</p>
<p>{{cs.toAcc.value}}</p>
<p>{{transactionForm.controls.toAcc.value}}</p>
<p class="font-weight-bold">With the amount of:</p>
<p>€ {{decimalString(cs.amount)}}</p>
<p>€ {{decimalString(transactionForm.controls.amount)}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="dismissModal()">Close</button>
Expand Down
58 changes: 54 additions & 4 deletions src/app/transaction-form/transaction-form.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,75 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormBuilder } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { SubmitButtonComponent } from 'bb-ui/components/submit-button/submit-button.component';
import { collapseTextChangeRangesAcrossMultipleVersions } from 'typescript';

import { TransactionFormComponent } from './transaction-form.component';

describe('TransactionFormComponent', () => {
let component: TransactionFormComponent;
let component2: ComponentFixture<SubmitButtonComponent>;

let fixture: ComponentFixture<TransactionFormComponent>;
let fixture2: ComponentFixture<SubmitButtonComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TransactionFormComponent ]
})
.compileComponents();
declarations: [TransactionFormComponent, SubmitButtonComponent],
providers: [FormBuilder]
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(TransactionFormComponent);
fixture2 = TestBed.createComponent(SubmitButtonComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
it('should create the TransactionFormComponent', () => {
expect(component).toBeTruthy();
});

it('should display validation errors if controls are empty on submit', () => {
component.transactionForm.controls.amount.setValue("")
component.transactionForm.controls.toAcc.setValue("")
component.submitClicked = true
fixture.detectChanges()

expect(component.transactionForm.valid).toBeFalsy()
expect(fixture.debugElement.queryAll(By.css('.error')).length).toBe(2)
});

it('should not display validation errors if controls are valid', () => {
component.transactionForm.controls.amount.setValue("123")
component.transactionForm.controls.toAcc.setValue("test account")
fixture.detectChanges()

expect(component.transactionForm.valid).toBeTruthy()
expect(fixture.debugElement.queryAll(By.css('.error')).length).toBe(0)
});

xit('should display validation errors if amount field is too large on submit', () => {
component.userBalance = 5000
component.transactionForm.controls.toAcc.setValue("test account")
component.transactionForm.controls.amount.setValue(5501)
component.transactionForm.updateValueAndValidity()
component.submitClicked = true

fixture.detectChanges()

expect(component.transactionForm.invalid).toBeTruthy()
expect(fixture.debugElement.queryAll(By.css('.error')).length).toBe(1)
});

it('should display validation errors if amount field is negative on submit', () => {
component.transactionForm.controls.toAcc.setValue("test account")
component.transactionForm.controls.amount.setValue("-1")
component.submitClicked = true
fixture.detectChanges()

expect(component.transactionForm.valid).toBeFalsy()
expect(fixture.debugElement.queryAll(By.css('.error')).length).toBe(1)
});
});
7 changes: 3 additions & 4 deletions src/app/transaction-form/transaction-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export class TransactionFormComponent {
userBalance: number = 5824.76
submitClicked: boolean = false
transactionForm: FormGroup = this.emptyTransactionForm()
cs = this.transactionForm.controls //TODO remove shorthand

private emptyTransactionForm() {
return this.formBuilder.group({
Expand All @@ -29,7 +28,6 @@ export class TransactionFormComponent {
private resetForm() {
this.transactionForm = this.emptyTransactionForm()
this.submitClicked = false
this.cs = this.transactionForm.controls
}

dismissModal() {
Expand All @@ -43,8 +41,9 @@ export class TransactionFormComponent {
}

onConfirm() {
this.userBalance -= this.cs.amount.value
this.newTransfer.emit({ 'amount': this.cs.amount.value, 'account': this.cs.toAcc.value })
const amount = this.transactionForm.controls.amount.value
this.userBalance -= amount
this.newTransfer.emit({ 'amount': amount, 'account': this.transactionForm.controls.toAcc.value })
this.resetForm()
this.dismissModal()
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/transaction-list/transaction-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="col-lg-2 col-auto">
<div class="row align-items-center">
<span class="code col px-0 py-3" [ngStyle]="{'background-color': t.categoryCode}">&nbsp;</span>
<span class="col">{{dateString(t.dates.valueDate)}}</span>
<span class="date col">{{dateString(t.dates.valueDate)}}</span>
</div>
</div>
<div class="col-lg-7 col">
Expand Down
3 changes: 2 additions & 1 deletion src/app/transaction-list/transaction-list.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

.list-container {
overflow: auto;
height: 68vh;
height: 66vh;
}

.item {
border-width: 0;
border-bottom-width: 1px;
border-radius: 0;
}

::-webkit-scrollbar {
Expand Down
55 changes: 40 additions & 15 deletions src/app/transaction-list/transaction-list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import { HttpClient, HttpHandler } from '@angular/common/http';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { By } from '@angular/platform-browser';
import { Transaction } from '../model';
import { TransactionListComponent } from './transaction-list.component';

describe('TransactionListComponent', () => {
let component: TransactionListComponent;
let fixture: ComponentFixture<TransactionListComponent>;
let component: TransactionListComponent
let fixture: ComponentFixture<TransactionListComponent>

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ TransactionListComponent ]
})
.compileComponents();
});
declarations: [TransactionListComponent],
providers: [HttpClient, HttpHandler]
}).compileComponents()
})

beforeEach(() => {
fixture = TestBed.createComponent(TransactionListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fixture = TestBed.createComponent(TransactionListComponent)
component = fixture.componentInstance
fixture.detectChanges()
})

it('should create the TransactionListComponent', () => {
expect(component).toBeTruthy()
})

it('should display a list entry for each transaction', () => {
component.transactions = [new Transaction(1), new Transaction(2), new Transaction(3)]
fixture.detectChanges()
expect(fixture.debugElement.queryAll(By.css('.list-group-item')).length).toBe(3)
})

it('should add a transaction to the list if the @Input is updated and ngOnChanges is called', () => {
component.transactions = [new Transaction(1), new Transaction(2), new Transaction(3)]
component.newTransaction = new Transaction(4)
component.ngOnChanges()
fixture.detectChanges()
expect(fixture.debugElement.queryAll(By.css('.list-group-item')).length).toBe(4)
})

it('should create', () => {
expect(component).toBeTruthy();
});
});
xit('should sort the transactions by date', () => {
component.transactions = [new Transaction(1, "", "", 0), new Transaction(2, "", "", 86400), new Transaction(3, "", "", 172800)]
fixture.detectChanges()
const list = fixture.debugElement.queryAll(By.css('.date'))
for (let i = 0; i < 3; i++) {
expect(list[i].nativeNode.innerHTML).toContain("Jan " + (i + 1))
}
})
})
2 changes: 1 addition & 1 deletion src/app/transaction-list/transaction-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TransactionListComponent implements OnInit, OnChanges {
}

private getData(): Observable<Transaction[]> {
const corsProxy = 'https://cors.bridged.cc/' //TODO
const corsProxy = 'https://cors.bridged.cc/'
const url = 'https://r9vdzv10vd.execute-api.eu-central-1.amazonaws.com/dev/transactions'
return this.http.get<Array<Transaction>>(corsProxy + url).pipe(
map((data: Transaction[]) => data),
Expand Down

0 comments on commit b0bad44

Please sign in to comment.