Commit ab6a5129 authored by Ben Hayward's avatar Ben Hayward

adding tests

1 merge request!460WIP: [Sprint/JollyJellyfish](feat) Plus tiers engine#578
Pipeline #74200299 canceled with stages
in 4 minutes and 32 seconds
......@@ -73,6 +73,10 @@ describe('ChannelsListComponent', () => {
// synchronous beforeEach
beforeEach((done) => {
jasmine.MAX_PRETTY_PRINT_DEPTH = 10;
jasmine.clock().uninstall();
jasmine.clock().install();
fixture = TestBed.createComponent(ChannelsListComponent);
comp = fixture.componentInstance;
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
......@@ -39,14 +39,34 @@ let overlayModal = new function () {
});
};
fdescribe('WirePaymentsCreatorComponent', () => {
describe('WirePaymentsCreatorComponent', () => {
let comp: PaymentPlanComponent;
let fixture: ComponentFixture<PaymentPlanComponent>;
function getButton(): DebugElement {
function getPurchaseButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__period-buy-button__blue'));
}
function getMonthlyOffchainButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__plans div:nth-child(1) input'))
}
function getYearlyOnchainButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__plans div:nth-child(2) span:nth-child(3) input'))
}
function getYearlyOffchainButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__plans div:nth-child(2) span:nth-child(2) input'))
}
function getLifeOnchainButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__plans div:nth-child(3) span:nth-child(3) input'))
}
function getLifeOffchainButton(): DebugElement {
return fixture.debugElement.query(By.css('.m-plus-plan__plans div:nth-child(3) span:nth-child(2) input'))
}
beforeEach(async(() => {
......@@ -84,11 +104,31 @@ fdescribe('WirePaymentsCreatorComponent', () => {
jasmine.MAX_PRETTY_PRINT_DEPTH = 10;
jasmine.clock().uninstall();
jasmine.clock().install();
fixture = TestBed.createComponent(PaymentPlanComponent);
window.Minds.blockchain = {
plus_address: 'oxtn'
plus_address: '0x00000000000000'
}
// comp.receiver = window.Minds.blockchain.plus_address;
clientMock.response = {};
clientMock.response[`api/v2/boost/rates`] = {
'balance': 301529,
'rate': 1,
'cap': 5000,
'min': 100,
'usd': 1000,
'tokens': 1
};
clientMock.response['api/v2/blockchain/wallet/balance'] = {
addresses: [{
balance: 1
},
{
balance: 1
}]
};
comp = fixture.componentInstance; // LoginForm test instance
fixture.detectChanges();
......@@ -103,53 +143,71 @@ fdescribe('WirePaymentsCreatorComponent', () => {
});
afterEach(() => {
TestBed.resetTestingModule();
jasmine.clock().uninstall();
});
it('should have a purchase button', () => {
const button = getButton();
const button = getPurchaseButton();
expect(button).not.toBeNull();
});
it('should create the payment object for monthly', () => {
const button = getButton();
spyOn(comp, 'submit').and.stub();
button.nativeElement.click();
expect(comp.submit).toHaveBeenCalledWith('month');
it('should have all radio buttons', () => {
expect(getMonthlyOffchainButton()).not.toBeNull();
expect(getYearlyOnchainButton()).not.toBeNull();
expect(getYearlyOffchainButton()).not.toBeNull();
expect(getLifeOnchainButton()).not.toBeNull();
expect(getLifeOffchainButton()).not.toBeNull();
});
it('should set the amount correctly for monthly offchain', () => {
comp.createWire('offchain month');
expect(comp.wire.amount).toBe(20);
it('radio buttons should change tier value', () => {
getMonthlyOffchainButton().nativeElement.click();
expect(comp.tier).toBe('offchain month');
getYearlyOffchainButton().nativeElement.click();
expect(comp.tier).toBe('offchain year');
getYearlyOnchainButton().nativeElement.click();
expect(comp.tier).toBe('onchain year');
getLifeOffchainButton().nativeElement.click();
expect(comp.tier).toBe('offchain lifetime');
getLifeOnchainButton().nativeElement.click();
expect(comp.tier).toBe('onchain lifetime');
});
it('should create the payment object for yearly', () => {
const button = getButton();
spyOn(comp, 'submit').and.stub();
button.nativeElement.click();
expect(comp.submit).toHaveBeenCalledWith('year');
});
it('should call submit on purchase button click', fakeAsync(() => {
spyOn(comp, 'submit').and.stub();
it('should set the amount correctly for yearly', () => {
comp.createWire('offchain month');
expect(comp.wire.amount).toBe(200);
});
getMonthlyOffchainButton().nativeElement.click();
getPurchaseButton().nativeElement.click();
expect(comp.submit).toHaveBeenCalled();
}));
it('should have a buy life button', () => {
const button = getButton();
expect(button).not.toBeNull();
});
it('should load the current balance', () => fakeAsync(() => {
comp.load();
tick();
expect(comp.inProgress).toBeFalsy();
expect(comp.rates).toBe({
'balance': 301529,
'rate': 1,
'cap': 5000,
'min': 100,
'usd': 1000,
'tokens': 1
});
}));
it('should create the payment object for life', () => {
const button = getButton();
spyOn(comp, 'submit').and.stub();
button.nativeElement.click();
expect(comp.submit).toHaveBeenCalledWith('lifetime')
});
it('should set the amount correctly for lifetime', () => {
comp.createWire('offchain lifetime');
expect(comp.wire.amount).toBe(500);
});
it('should create the wire object correctly', fakeAsync(() => {
comp.createWire('offchain month');
tick();
expect(comp.wire).not.toBeNull();
expect(comp.wire.amount).toBe(20);
expect(comp.wire.recurring).toBeFalsy();
expect(comp.wire.guid).not.toBeNull();
}));
});
......@@ -12,6 +12,14 @@ import { TokenContractService } from '../../blockchain/contracts/token-contract.
export type PayloadType = 'onchain' | 'offchain';
/**
* Component containing the logic for payment plan component,
* allowing users to select a plus tier, pay and subscribe.
*
* Based on the creator component.
* @author Ben Hayward
*/
@Component({
providers: [CurrencyPipe],
selector: 'm-payment-plan',
......@@ -121,6 +129,9 @@ export class PaymentPlanComponent {
}
/**
* Loads balances for the current wallet.
*/
async loadBalances() {
try {
let currentWallet = await this.web3Wallet.getCurrentWallet();
......@@ -149,6 +160,11 @@ export class PaymentPlanComponent {
}
}
/**
* Loads current balance by wallet address.
* @param {string} address - wallet address
*/
async loadCurrentWalletBalance(address) {
try {
this.balances.onChainAddress = address;
......@@ -162,6 +178,9 @@ export class PaymentPlanComponent {
}
}
/**
* Loads the current rate of tokens.
*/
async loadTokenRate() {
let response: any = await this.client.get(`api/v2/blockchain/rate/tokens`);
......@@ -170,6 +189,7 @@ export class PaymentPlanComponent {
}
}
/**
* Populates the wire object
* .
......@@ -207,6 +227,10 @@ export class PaymentPlanComponent {
this.wire.guid = this.minds.blockchain.plus_guid;
}
/**
* Called on form submission. Submits the payment and returns success or an error.
*/
async submit() {
this.createWire(this.tier);
......@@ -257,6 +281,7 @@ export class PaymentPlanComponent {
}
}
/**
* Checks if the user can submit using the current settings
*/
......@@ -271,7 +296,8 @@ export class PaymentPlanComponent {
return false;
}
/**
/**
* Validates if the wire payment can be submitted using the current settings
*/
validate() {
......@@ -311,6 +337,10 @@ export class PaymentPlanComponent {
}
}
/**
* Resets to default values.
*/
setDefaults() {
this.wire.recurring = true;
let payloadType = localStorage.getItem('preferred-payment-method');
......@@ -320,6 +350,7 @@ export class PaymentPlanComponent {
this.setPayloadType(<PayloadType>payloadType);
}
/**
* Sets the wire currency
*/
......@@ -338,6 +369,7 @@ export class PaymentPlanComponent {
this.showErrors();
}
/**
* Sets the wire payment nonce
*/
......@@ -346,6 +378,7 @@ export class PaymentPlanComponent {
this.showErrors();
}
/**
* Sets the onchain specific wire payment nonce
*/
......@@ -353,6 +386,7 @@ export class PaymentPlanComponent {
return this.setNoncePayload({ receiver: this.blockchain.plus_address, address })
}
/**
* Round by 4
*/
......@@ -360,6 +394,7 @@ export class PaymentPlanComponent {
this.wire.amount = Math.round(parseFloat(`${this.wire.amount}`) * 10000) / 10000;
}
/**
* Shows visible wire errors
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment