|
| 1 | +import React from 'react'; |
| 2 | +import * as LOOP from 'types/generated/loop_pb'; |
| 3 | +import { fireEvent } from '@testing-library/react'; |
| 4 | +import { ellipseInside } from 'util/strings'; |
| 5 | +import { renderWithProviders } from 'util/tests'; |
| 6 | +import { loopListSwaps } from 'util/tests/sampleData'; |
| 7 | +import { createStore, Store } from 'store'; |
| 8 | +import { Swap } from 'store/models'; |
| 9 | +import ProcessingSwaps from 'components/loop/processing/ProcessingSwaps'; |
| 10 | + |
| 11 | +const { LOOP_IN, LOOP_OUT } = LOOP.SwapType; |
| 12 | +const { |
| 13 | + INITIATED, |
| 14 | + PREIMAGE_REVEALED, |
| 15 | + HTLC_PUBLISHED, |
| 16 | + SUCCESS, |
| 17 | + INVOICE_SETTLED, |
| 18 | + FAILED, |
| 19 | +} = LOOP.SwapState; |
| 20 | +const width = (el: any) => window.getComputedStyle(el).width; |
| 21 | + |
| 22 | +describe('ProcessingSwaps component', () => { |
| 23 | + let store: Store; |
| 24 | + |
| 25 | + const addSwap = (type: number, state: number, id?: string) => { |
| 26 | + const swap = new Swap(loopListSwaps.swapsList[0]); |
| 27 | + swap.id = `${id || ''}${swap.id}`; |
| 28 | + swap.type = type; |
| 29 | + swap.state = state; |
| 30 | + swap.lastUpdateTime = Date.now() * 1000 * 1000; |
| 31 | + store.swapStore.swaps.set(swap.id, swap); |
| 32 | + return swap; |
| 33 | + }; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + store = createStore(); |
| 37 | + }); |
| 38 | + |
| 39 | + const render = () => { |
| 40 | + return renderWithProviders(<ProcessingSwaps />, store); |
| 41 | + }; |
| 42 | + |
| 43 | + it('should display the title', async () => { |
| 44 | + const { getByText } = render(); |
| 45 | + expect(getByText('Processing Loops')).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should display an INITIATED Loop In', () => { |
| 49 | + const { getByText, getByTitle } = render(); |
| 50 | + const swap = addSwap(LOOP_IN, INITIATED); |
| 51 | + expect(getByText('dot.svg')).toHaveClass('warn'); |
| 52 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 53 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 54 | + expect(width(getByTitle(swap.stateLabel))).toBe('25%'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should display an HTLC_PUBLISHED Loop In', () => { |
| 58 | + const { getByText, getByTitle } = render(); |
| 59 | + const swap = addSwap(LOOP_IN, HTLC_PUBLISHED); |
| 60 | + expect(getByText('dot.svg')).toHaveClass('warn'); |
| 61 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 62 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 63 | + expect(width(getByTitle(swap.stateLabel))).toBe('50%'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should display an INVOICE_SETTLED Loop In', () => { |
| 67 | + const { getByText, getByTitle } = render(); |
| 68 | + const swap = addSwap(LOOP_IN, INVOICE_SETTLED); |
| 69 | + expect(getByText('dot.svg')).toHaveClass('warn'); |
| 70 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 71 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 72 | + expect(width(getByTitle(swap.stateLabel))).toBe('75%'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should display an SUCCESS Loop In', () => { |
| 76 | + const { getByText, getByTitle } = render(); |
| 77 | + const swap = addSwap(LOOP_IN, SUCCESS); |
| 78 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 79 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 80 | + expect(width(getByTitle(swap.stateLabel))).toBe('100%'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should display an FAILED Loop In', () => { |
| 84 | + const { getByText } = render(); |
| 85 | + const swap = addSwap(LOOP_IN, FAILED); |
| 86 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 87 | + expect(getByText(swap.stateLabel)).toBeInTheDocument(); |
| 88 | + expect(getByText('close.svg')).toBeInTheDocument(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should display an INITIATED Loop Out', () => { |
| 92 | + const { getByText, getByTitle } = render(); |
| 93 | + const swap = addSwap(LOOP_OUT, INITIATED); |
| 94 | + expect(getByText('dot.svg')).toHaveClass('warn'); |
| 95 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 96 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 97 | + expect(width(getByTitle(swap.stateLabel))).toBe('33%'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should display an PREIMAGE_REVEALED Loop Out', () => { |
| 101 | + const { getByText, getByTitle } = render(); |
| 102 | + const swap = addSwap(LOOP_OUT, PREIMAGE_REVEALED); |
| 103 | + expect(getByText('dot.svg')).toHaveClass('warn'); |
| 104 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 105 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 106 | + expect(width(getByTitle(swap.stateLabel))).toBe('66%'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should display an SUCCESS Loop Out', () => { |
| 110 | + const { getByText, getByTitle } = render(); |
| 111 | + const swap = addSwap(LOOP_OUT, SUCCESS); |
| 112 | + expect(getByText('dot.svg')).toHaveClass('success'); |
| 113 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 114 | + expect(getByTitle(swap.stateLabel)).toBeInTheDocument(); |
| 115 | + expect(width(getByTitle(swap.stateLabel))).toBe('100%'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should display an FAILED Loop Out', () => { |
| 119 | + const { getByText } = render(); |
| 120 | + const swap = addSwap(LOOP_OUT, FAILED); |
| 121 | + expect(getByText(ellipseInside(swap.id))).toBeInTheDocument(); |
| 122 | + expect(getByText(swap.stateLabel)).toBeInTheDocument(); |
| 123 | + expect(getByText('close.svg')).toBeInTheDocument(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should dismiss a failed Loop', () => { |
| 127 | + const { getByText } = render(); |
| 128 | + addSwap(LOOP_OUT, FAILED); |
| 129 | + expect(store.swapStore.dismissedSwapIds).toHaveLength(0); |
| 130 | + fireEvent.click(getByText('close.svg')); |
| 131 | + expect(store.swapStore.dismissedSwapIds).toHaveLength(1); |
| 132 | + }); |
| 133 | +}); |
0 commit comments