2021-04-30 10:33:36 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import Vuex from 'vuex';
|
|
|
|
|
2021-06-16 19:36:34 +01:00
|
|
|
import { RootState } from '@/app/store';
|
2021-05-31 16:40:04 +01:00
|
|
|
import { Expectation, HeldAmountSummary, PayoutsSummary, Paystub } from '@/payouts';
|
2021-04-30 10:33:36 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
|
|
|
|
|
|
|
import store, { payoutsService } from '../mock/store';
|
|
|
|
|
2021-06-16 19:36:34 +01:00
|
|
|
const state = store.state as RootState;
|
2021-04-30 10:33:36 +01:00
|
|
|
|
|
|
|
const summary = new PayoutsSummary(5000000, 6000000, 9000000);
|
2021-05-31 16:40:04 +01:00
|
|
|
const expectations = new Expectation(4000000, 3000000);
|
|
|
|
const expectationsByNode = new Expectation(1000000, 2000000);
|
2021-04-30 10:33:36 +01:00
|
|
|
const period = '2021-04';
|
2021-05-31 16:40:04 +01:00
|
|
|
const totalPaystub = new Paystub(2000);
|
2021-08-02 18:09:54 +01:00
|
|
|
|
2021-05-31 16:40:04 +01:00
|
|
|
totalPaystub.paid = 40000;
|
2021-08-02 18:09:54 +01:00
|
|
|
|
2021-05-31 16:40:04 +01:00
|
|
|
const paystub = new Paystub(3000);
|
|
|
|
const heldHistory = [
|
|
|
|
new HeldAmountSummary('satelliteName', 1000, 2000, 3000, 10),
|
|
|
|
new HeldAmountSummary('satelliteName', 2000, 3000, 4000, 20),
|
|
|
|
];
|
2021-04-30 10:33:36 +01:00
|
|
|
|
|
|
|
describe('mutations', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets summary', () => {
|
|
|
|
store.commit('payouts/setSummary', summary);
|
|
|
|
|
|
|
|
expect(state.payouts.summary.totalPaid).toBe(summary.totalPaid);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets payout period', () => {
|
|
|
|
store.commit('payouts/setPayoutPeriod', period);
|
|
|
|
|
|
|
|
expect(state.payouts.selectedPayoutPeriod).toBe(period);
|
|
|
|
});
|
2021-05-21 16:28:49 +01:00
|
|
|
|
|
|
|
it('sets total expectations', () => {
|
|
|
|
store.commit('payouts/setTotalExpectation', expectations);
|
|
|
|
|
|
|
|
expect(state.payouts.totalExpectations.currentMonthEstimation).toBe(expectations.currentMonthEstimation);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets selected node expectations', () => {
|
|
|
|
store.commit('payouts/setCurrentNodeExpectations', expectationsByNode);
|
|
|
|
|
2021-05-31 16:40:04 +01:00
|
|
|
expect(state.payouts.selectedNodePayouts.expectations.currentMonthEstimation).toBe(expectationsByNode.currentMonthEstimation);
|
2021-05-21 16:28:49 +01:00
|
|
|
});
|
2021-04-30 10:33:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('actions', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
store.commit('payouts/setSummary', new PayoutsSummary());
|
2021-05-31 16:40:04 +01:00
|
|
|
store.commit('payouts/setCurrentNodeExpectations', new Expectation());
|
|
|
|
store.commit('payouts/setPayoutPeriod', null);
|
|
|
|
store.commit('payouts/setTotalExpectation', new Expectation());
|
|
|
|
store.commit('payouts/setNodeTotals', new Paystub());
|
|
|
|
store.commit('payouts/setNodePaystub', new Paystub());
|
|
|
|
store.commit('payouts/setNodeHeldHistory', []);
|
2021-04-30 10:33:36 +01:00
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('throws error on failed summary fetch', async() => {
|
2021-04-30 10:33:36 +01:00
|
|
|
jest.spyOn(payoutsService, 'summary').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch('payouts/summary');
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payouts.summary.totalPaid).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches payouts summary', async() => {
|
2021-04-30 10:33:36 +01:00
|
|
|
jest.spyOn(payoutsService, 'summary').mockReturnValue(
|
|
|
|
Promise.resolve(summary),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch('payouts/summary');
|
|
|
|
|
|
|
|
expect(state.payouts.summary.totalPaid).toBe(summary.totalPaid);
|
|
|
|
});
|
2021-05-21 16:28:49 +01:00
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('throws error on failed expectations fetch', async() => {
|
2021-05-21 16:28:49 +01:00
|
|
|
jest.spyOn(payoutsService, 'expectations').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch('payouts/expectations');
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.payouts.totalExpectations.undistributed).toBe(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches total expectations', async() => {
|
2021-05-21 16:28:49 +01:00
|
|
|
jest.spyOn(payoutsService, 'expectations').mockReturnValue(
|
|
|
|
Promise.resolve(expectations),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch('payouts/expectations');
|
|
|
|
|
|
|
|
expect(state.payouts.totalExpectations.undistributed).toBe(expectations.undistributed);
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches by node expectations', async() => {
|
2021-05-21 16:28:49 +01:00
|
|
|
jest.spyOn(payoutsService, 'expectations').mockReturnValue(
|
|
|
|
Promise.resolve(expectationsByNode),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch('payouts/expectations', 'nodeId');
|
|
|
|
|
|
|
|
expect(state.payouts.totalExpectations.undistributed).toBe(0);
|
2021-05-31 16:40:04 +01:00
|
|
|
expect(state.payouts.selectedNodePayouts.expectations.undistributed).toBe(expectationsByNode.undistributed);
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches paystubs for period', async() => {
|
2021-05-31 16:40:04 +01:00
|
|
|
jest.spyOn(payoutsService, 'paystub').mockReturnValue(
|
|
|
|
Promise.resolve(paystub),
|
|
|
|
);
|
|
|
|
|
|
|
|
store.commit('payouts/setPayoutPeriod', period);
|
|
|
|
await store.dispatch('payouts/paystub', 'nodeId');
|
|
|
|
|
|
|
|
expect(state.payouts.selectedNodePayouts.paystubForPeriod.usageAtRest).toBe(paystub.usageAtRest);
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches total paystub', async() => {
|
2021-05-31 16:40:04 +01:00
|
|
|
jest.spyOn(payoutsService, 'paystub').mockReturnValue(
|
|
|
|
Promise.resolve(totalPaystub),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch('payouts/nodeTotals', 'nodeId');
|
|
|
|
|
|
|
|
expect(state.payouts.selectedNodePayouts.totalEarned).toBe(totalPaystub.paid);
|
|
|
|
});
|
|
|
|
|
2021-08-02 18:09:54 +01:00
|
|
|
it('success fetches total paystub', async() => {
|
2021-05-31 16:40:04 +01:00
|
|
|
jest.spyOn(payoutsService, 'heldHistory').mockReturnValue(
|
|
|
|
Promise.resolve(heldHistory),
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch('payouts/heldHistory', 'nodeId');
|
|
|
|
|
|
|
|
expect(state.payouts.selectedNodePayouts.heldHistory.length).toBe(heldHistory.length);
|
|
|
|
expect(state.payouts.selectedNodePayouts.heldHistory[0].periodCount).toBe(heldHistory[0].periodCount);
|
2021-05-21 16:28:49 +01:00
|
|
|
});
|
2021-04-30 10:33:36 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2021-08-02 18:09:54 +01:00
|
|
|
it('getter monthsOnNetwork returns correct value', () => {
|
2021-04-30 10:33:36 +01:00
|
|
|
store.commit('payouts/setPayoutPeriod', period);
|
|
|
|
|
|
|
|
expect(store.getters['payouts/periodString']).toBe('April, 2021');
|
|
|
|
});
|
|
|
|
});
|