import {Spi} from '../src/Spi'; import {SpiStatus} from '../src/SpiModels'; import {SpiPayAtTable} from '../src/SpiPayAtTable'; import {BillStatusResponse, BillRetrievalResult, GetOpenTablesResponse, OpenTablesEntry} from '../src/PayAtTable'; import {Message} from '../src/Messages'; import BillStatusRequestFixture from './fixtures/BillStatusRequest.json' import BillPaymentWithCardFixture from './fixtures/BillPaymentWithCard.json' describe('SpiPayAtTable', function() { 'use strict'; it('should setup a new pay at table instance', () => { const spi = new Spi('TABLEPOS1', 'localhost', null, null); const payAtTable = new SpiPayAtTable(spi); expect(payAtTable._spi).toBeDefined(); expect(payAtTable.Config).toBeDefined(); expect(payAtTable.Config.TableRetrievalEnabled).toBeFalsy(); }); describe("PushPayAtTableConfig()", () => { it("should not push pay at table config when not paired", () => { // arrange const spi = new Spi("TABLEPOS1", "", "localhost", null); const payAtTable = new SpiPayAtTable(spi); spi.CurrentStatus = SpiStatus.Unpaired; spi._send = () => true; spyOn(spi, "_send"); // act payAtTable.PushPayAtTableConfig(); // assert expect(spi._send).not.toHaveBeenCalled(); }); it("should push pay at table config when paired", () => { // arrange const spi = new Spi("TABLEPOS1", "", "localhost", null); const payAtTable = new SpiPayAtTable(spi); spi.CurrentStatus = SpiStatus.PairedConnected; spi._send = () => true; spyOn(spi, "_send"); // act payAtTable.PushPayAtTableConfig(); // assert expect(spi._send).toHaveBeenCalledWith(jasmine.objectContaining({ EventName: 'set_table_config' })); }); }); it('should open a table and correctly return a response with the open table', () => { // arrange const openTablesEntries = [ new OpenTablesEntry({ TableId: '1', Label: '1', BillOutstandingAmount: 2000, }), ]; // act const getOpenTablesResponse = new GetOpenTablesResponse(openTablesEntries); const openTablesResponse = getOpenTablesResponse.GetOpenTables(); // assert expect(openTablesResponse.length).toBe(openTablesEntries.length); }); it('should return an empty array when the open table data is null', () => { // arrange const getOpenTablesResponse = new GetOpenTablesResponse(null) // act const openTablesResponse = getOpenTablesResponse.GetOpenTables(); // assert expect(openTablesResponse.length).toBe(0); expect(getOpenTablesResponse.TableData).toBeNull(); }); it('should handle bill status request', async () => { const request = JSON.stringify(BillStatusRequestFixture); const spi = new Spi('TABLEPOS1', 'localhost', null, null); spi._send = () => true; spyOn(spi,'_send'); const payAtTable = new SpiPayAtTable(spi); const message = Message.FromJson(request, null); payAtTable.GetBillStatus = function(billId, tableId, operatorId) { return Object.assign(new BillStatusResponse(), { Result: BillRetrievalResult.SUCCESS, BillId: billId, TableId: tableId, TotalAmount: 100, OutstandingAmount: 100 }); }; await payAtTable._handleGetBillDetailsRequest(message); expect(spi._send).toHaveBeenCalledWith(jasmine.objectContaining({ EventName: 'bill_details' })); }); it('should handle bill payment advice', async () => { const request = JSON.stringify(BillPaymentWithCardFixture); const spi = new Spi('TABLEPOS1', 'localhost', null, null); spi._send = () => true; spyOn(spi,'_send'); const payAtTable = new SpiPayAtTable(spi); const message = Message.FromJson(request, null); payAtTable.GetBillStatus = function(billId, tableId, operatorId) { return Object.assign(new BillStatusResponse(), { Result: BillRetrievalResult.SUCCESS, BillId: billId, TableId: tableId, TotalAmount: 100, OutstandingAmount: 100 }); }; payAtTable.BillPaymentReceived = function(billPayment, updatedBillData) { return Object.assign(new BillStatusResponse(), { Result: BillRetrievalResult.SUCCESS, OutstandingAmount: 10, TotalAmount: 10 }); }; await payAtTable._handleBillPaymentAdvice(message); expect(spi._send).toHaveBeenCalledWith(jasmine.objectContaining({ EventName: 'bill_details' })); }); describe("_handleBillPaymentFlowEnded()", async () => { it("should not handle BillPaymentFlowEnded when not paired", async () => { // arrange const spi = new Spi("TABLEPOS1", "", "localhost", null); const spiPayAtTable = new SpiPayAtTable(spi); spi.CurrentStatus = SpiStatus.Unpaired; spi._send = () => true; spyOn(spi, "_send"); spiPayAtTable.BillPaymentFlowEnded = () => true; // act return spiPayAtTable._handleBillPaymentFlowEnded({Data: {}}).then(() => { // assert expect(spi._send).not.toHaveBeenCalled(); }); }); it("should handle BillPaymentFlowEnded when paired", async () => { // arrange const billMessage = { Data: { bill_id: '100', } }; const spi = new Spi("TABLEPOS1", "", "localhost", null); const spiPayAtTable = new SpiPayAtTable(spi); spi.CurrentStatus = SpiStatus.PairedConnected; spi._send = () => true; spyOn(spi, "_send"); spiPayAtTable.BillPaymentFlowEnded = () => true; // act return spiPayAtTable._handleBillPaymentFlowEnded(billMessage).then(() => { // assert expect(spi._send).toHaveBeenCalledWith(jasmine.objectContaining({ EventName: 'bill_payment_flow_ended_ack' })); }); }); }); });