|
| 1 | +import { isDeepObjectMatch } from '../../src/util/websockets/WsStore'; |
| 2 | + |
| 3 | +describe('WsStore', () => { |
| 4 | + describe('isDeepObjectMatch()', () => { |
| 5 | + it('should match two equal strings', () => { |
| 6 | + expect( |
| 7 | + isDeepObjectMatch('orderbook.50.BTCUSDT', 'orderbook.50.BTCUSDT'), |
| 8 | + ).toBeTruthy(); |
| 9 | + expect( |
| 10 | + isDeepObjectMatch('orderbook.50.BTCUSDT', 'orderbook.50.ETHUSDT'), |
| 11 | + ).toBeFalsy(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should match simple topic objects', () => { |
| 15 | + const topic1 = { |
| 16 | + topic: 'orderbook.50.BTCUSDT', |
| 17 | + }; |
| 18 | + const topic2 = { |
| 19 | + topic: 'orderbook.50.BTCUSDT', |
| 20 | + }; |
| 21 | + |
| 22 | + expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should match topic objects with payload, even if keys are differently ordered', () => { |
| 26 | + const topic1 = { |
| 27 | + topic: 'orderbook.50.BTCUSDT', |
| 28 | + payload: { symbol: 'BTCUSDT' }, |
| 29 | + category: 'linear', |
| 30 | + }; |
| 31 | + const topic2 = { |
| 32 | + category: 'linear', |
| 33 | + payload: { symbol: 'BTCUSDT' }, |
| 34 | + topic: 'orderbook.50.BTCUSDT', |
| 35 | + }; |
| 36 | + |
| 37 | + expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should match nested payload objects', () => { |
| 41 | + const topic1 = { |
| 42 | + topic: 'kline.5.BTCUSDT', |
| 43 | + payload: { |
| 44 | + symbol: 'BTCUSDT', |
| 45 | + interval: '5', |
| 46 | + }, |
| 47 | + category: 'spot', |
| 48 | + }; |
| 49 | + const topic2 = { |
| 50 | + topic: 'kline.5.BTCUSDT', |
| 51 | + payload: { |
| 52 | + symbol: 'BTCUSDT', |
| 53 | + interval: '5', |
| 54 | + }, |
| 55 | + category: 'spot', |
| 56 | + }; |
| 57 | + |
| 58 | + expect(isDeepObjectMatch(topic1, topic2)).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should NOT match topics with different category', () => { |
| 62 | + const topic1 = { |
| 63 | + topic: 'orderbook.50.BTCUSDT', |
| 64 | + category: 'linear', |
| 65 | + }; |
| 66 | + const topic2 = { |
| 67 | + topic: 'orderbook.50.BTCUSDT', |
| 68 | + category: 'spot', |
| 69 | + }; |
| 70 | + |
| 71 | + expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should NOT match topics with different payload values', () => { |
| 75 | + const topic1 = { |
| 76 | + topic: 'kline.5.BTCUSDT', |
| 77 | + payload: { symbol: 'BTCUSDT' }, |
| 78 | + category: 'spot', |
| 79 | + }; |
| 80 | + const topic2 = { |
| 81 | + topic: 'kline.5.BTCUSDT', |
| 82 | + payload: { symbol: 'ETHUSDT' }, |
| 83 | + category: 'spot', |
| 84 | + }; |
| 85 | + |
| 86 | + expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should NOT match topics with nested payload differences', () => { |
| 90 | + const topic1 = { |
| 91 | + topic: 'kline.5.BTCUSDT', |
| 92 | + payload: { |
| 93 | + symbol: 'BTCUSDT', |
| 94 | + interval: '5', |
| 95 | + }, |
| 96 | + category: 'spot', |
| 97 | + }; |
| 98 | + const topic2 = { |
| 99 | + topic: 'kline.5.BTCUSDT', |
| 100 | + payload: { |
| 101 | + symbol: 'BTCUSDT', |
| 102 | + interval: '15', |
| 103 | + }, |
| 104 | + category: 'spot', |
| 105 | + }; |
| 106 | + |
| 107 | + expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should NOT match asymmetric objects (missing category property)', () => { |
| 111 | + const topic1 = { |
| 112 | + topic: 'orderbook.50.BTCUSDT', |
| 113 | + category: 'linear', |
| 114 | + }; |
| 115 | + const topic2 = { |
| 116 | + topic: 'orderbook.50.BTCUSDT', |
| 117 | + }; |
| 118 | + |
| 119 | + expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should NOT match asymmetric objects (missing nested property)', () => { |
| 123 | + const topic1 = { |
| 124 | + topic: 'kline.5.BTCUSDT', |
| 125 | + payload: { |
| 126 | + symbol: 'BTCUSDT', |
| 127 | + interval: '5', |
| 128 | + }, |
| 129 | + category: 'spot', |
| 130 | + }; |
| 131 | + const topic2 = { |
| 132 | + topic: 'kline.5.BTCUSDT', |
| 133 | + payload: { |
| 134 | + symbol: 'BTCUSDT', |
| 135 | + }, |
| 136 | + category: 'spot', |
| 137 | + }; |
| 138 | + |
| 139 | + expect(isDeepObjectMatch(topic1, topic2)).toBeFalsy(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should NOT match string to object', () => { |
| 143 | + expect( |
| 144 | + isDeepObjectMatch('orderbook.50.BTCUSDT', { |
| 145 | + topic: 'orderbook.50.BTCUSDT', |
| 146 | + }), |
| 147 | + ).toBeFalsy(); |
| 148 | + }); |
| 149 | + }); |
| 150 | +}); |
0 commit comments