|
| 1 | +import 'jsdom-global/register'; |
| 2 | +import React from 'react'; |
| 3 | +import test from 'tape'; |
| 4 | +import { findDOMNode, unmountComponentAtNode } from 'react-dom'; |
| 5 | +import { renderIntoDocument } from 'react-addons-test-utils'; |
| 6 | + |
| 7 | +import connect from '../connect'; |
| 8 | + |
| 9 | +test('Should throw if no firebase instance was found in either props or context', assert => { |
| 10 | + assert.throws(() => { |
| 11 | + const WrappedComponent = connect()('div'); |
| 12 | + |
| 13 | + renderIntoDocument(<WrappedComponent />); |
| 14 | + }, /Could not find "firebase"/); |
| 15 | + |
| 16 | + assert.end(); |
| 17 | +}); |
| 18 | + |
| 19 | +test('Should subscribe to a single path', assert => { |
| 20 | + const mockFirebase = { |
| 21 | + child: path => { |
| 22 | + assert.equal(path, 'foo'); |
| 23 | + |
| 24 | + return mockFirebase; |
| 25 | + }, |
| 26 | + on: (event, callback) => { |
| 27 | + assert.equal(event, 'value'); |
| 28 | + |
| 29 | + const mockSnapshot = { |
| 30 | + val: () => 'foo changed', |
| 31 | + }; |
| 32 | + |
| 33 | + callback(mockSnapshot); |
| 34 | + }, |
| 35 | + }; |
| 36 | + |
| 37 | + const mapPropsToSubscriptions = () => ({ foo: 'foo' }); |
| 38 | + const WrappedComponent = connect(mapPropsToSubscriptions)('div'); |
| 39 | + const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />); |
| 40 | + |
| 41 | + assert.deepEqual(container.state.subscriptionsState, { foo: 'foo changed' }); |
| 42 | + assert.end(); |
| 43 | +}); |
| 44 | + |
| 45 | +test('Should subscribe to a query', assert => { |
| 46 | + const mockFirebase = { |
| 47 | + child: () => { |
| 48 | + assert.fail(); |
| 49 | + |
| 50 | + return mockFirebase; |
| 51 | + }, |
| 52 | + startAt: priority => { |
| 53 | + assert.equal(priority, 1); |
| 54 | + |
| 55 | + return mockFirebase; |
| 56 | + }, |
| 57 | + on: (event, callback) => { |
| 58 | + assert.equal(event, 'value'); |
| 59 | + |
| 60 | + const mockSnapshot = { |
| 61 | + val: () => 'bar changed', |
| 62 | + }; |
| 63 | + |
| 64 | + callback(mockSnapshot); |
| 65 | + }, |
| 66 | + }; |
| 67 | + |
| 68 | + const mapPropsToSubscriptions = () => ({ bar: firebase => firebase.startAt(1) }); |
| 69 | + const WrappedComponent = connect(mapPropsToSubscriptions)('div'); |
| 70 | + const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />); |
| 71 | + |
| 72 | + assert.deepEqual(container.state.subscriptionsState, { bar: 'bar changed' }); |
| 73 | + assert.end(); |
| 74 | +}); |
| 75 | + |
| 76 | +test('Should unsubscribe when component unmounts', assert => { |
| 77 | + const mockFirebase = { |
| 78 | + child: path => { |
| 79 | + assert.equal(path, 'baz'); |
| 80 | + |
| 81 | + return mockFirebase; |
| 82 | + }, |
| 83 | + on: (event, callback) => { |
| 84 | + assert.equal(event, 'value'); |
| 85 | + |
| 86 | + const mockSnapshot = { |
| 87 | + val: () => 'baz changed', |
| 88 | + }; |
| 89 | + |
| 90 | + callback(mockSnapshot); |
| 91 | + }, |
| 92 | + off: event => { |
| 93 | + assert.equal(event, 'value'); |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + const mapPropsToSubscriptions = () => ({ baz: 'baz' }); |
| 98 | + const WrappedComponent = connect(mapPropsToSubscriptions)('div'); |
| 99 | + const container = renderIntoDocument(<WrappedComponent firebase={mockFirebase} />); |
| 100 | + |
| 101 | + assert.deepEqual(container.state.subscriptionsState, { baz: 'baz changed' }); |
| 102 | + |
| 103 | + unmountComponentAtNode(findDOMNode(container).parentNode); |
| 104 | + |
| 105 | + assert.end(); |
| 106 | +}); |
| 107 | + |
| 108 | +test('Should subscribe to nested paths'); |
| 109 | +test('Should map firebase to props'); |
| 110 | +test('Should keep connection to Firebase alive when specified and ignore later updates'); |
| 111 | +test('Should update subscriptions when props change'); |
| 112 | +test('Should not re-render if options.pure is true'); |
| 113 | +test('Should re-render if options.pure is false'); |
| 114 | +test('Should merge using mergeProps function'); |
0 commit comments