|
| 1 | +import { |
| 2 | + attachListener, |
| 3 | + getQueryConfigs, |
| 4 | + firestoreRef, |
| 5 | +} from '../../../src/utils/query'; |
| 6 | + |
| 7 | +let dispatch; |
| 8 | +let meta; |
| 9 | +let result; |
| 10 | + |
| 11 | +describe('query utils', () => { |
| 12 | + describe('attachListener', () => { |
| 13 | + it('is exported', () => { |
| 14 | + expect(attachListener).to.be.a('function'); |
| 15 | + }); |
| 16 | + |
| 17 | + describe('converts slash path to dot path', () => { |
| 18 | + beforeEach(() => { |
| 19 | + dispatch = sinon.spy(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('for collection', () => { |
| 23 | + meta = { collection: 'test' }; |
| 24 | + attachListener({ _: { listeners: {} } }, dispatch, meta); |
| 25 | + expect(dispatch).to.be.calledWith({ |
| 26 | + meta, |
| 27 | + payload: { name: 'test' }, |
| 28 | + type: '@@reduxFirestore/SET_LISTENER', |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('for collection and document', () => { |
| 33 | + meta = { collection: 'test', doc: 'doc' }; |
| 34 | + attachListener({ _: { listeners: {} } }, dispatch, meta); |
| 35 | + expect(dispatch).to.be.calledWith({ |
| 36 | + meta, |
| 37 | + payload: { name: `${meta.collection}/${meta.doc}` }, |
| 38 | + type: '@@reduxFirestore/SET_LISTENER', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('for collection, document, and subcollections', () => { |
| 43 | + meta = { collection: 'test', doc: 'doc', subcollections: [{ collection: 'test' }] }; |
| 44 | + attachListener({ _: { listeners: {} } }, dispatch, meta); |
| 45 | + expect(dispatch).to.be.calledWith({ |
| 46 | + meta, |
| 47 | + payload: { name: `${meta.collection}/${meta.doc}/${meta.subcollections[0].collection}` }, |
| 48 | + type: '@@reduxFirestore/SET_LISTENER', |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('throws if meta is not included', () => { |
| 54 | + expect(() => attachListener({}, dispatch)) |
| 55 | + .to.Throw('Meta data is required to attach listener.'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('throws if _ variable is not defined on Firebase', () => { |
| 59 | + expect(() => attachListener({}, dispatch, { collection: 'test' })) |
| 60 | + .to.Throw('Internal Firebase object required to attach listener. Confirm that reduxFirestore enhancer was added when you were creating your store'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('getQueryConfigs', () => { |
| 65 | + it('is exported', () => { |
| 66 | + expect(getQueryConfigs).to.be.a('function'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('it throws for invalid input', () => { |
| 70 | + expect(() => getQueryConfigs(1)) |
| 71 | + .to.Throw('Querie(s) must be an Array or a string'); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('array', () => { |
| 75 | + it('with collection in string', () => { |
| 76 | + expect(getQueryConfigs(['test'])) |
| 77 | + .to.have.nested.property('0.collection', 'test'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('with collection in an object', () => { |
| 81 | + expect(getQueryConfigs([{ collection: 'test' }])) |
| 82 | + .to.have.nested.property('0.collection', 'test'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('with collection and doc in an object', () => { |
| 86 | + meta = [{ collection: 'test', doc: 'other' }]; |
| 87 | + result = getQueryConfigs(meta); |
| 88 | + expect(result) |
| 89 | + .to.have.nested.property('0.collection', meta[0].collection); |
| 90 | + expect(result).to.have.nested.property('0.doc', meta[0].doc); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws invalid object', () => { |
| 94 | + meta = [{ test: 'test' }]; |
| 95 | + expect(() => getQueryConfigs(meta)) |
| 96 | + .to.Throw('Collection and/or Doc are required parameters within query definition object'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('string', () => { |
| 101 | + it('with collection', () => { |
| 102 | + expect(getQueryConfigs('test')) |
| 103 | + .to.have.property('collection', 'test'); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('object', () => { |
| 108 | + it('with collection', () => { |
| 109 | + expect(getQueryConfigs({ collection: 'test' })) |
| 110 | + .to.have.nested.property('0.collection', 'test'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('with doc', () => { |
| 114 | + meta = { collection: 'test', doc: 'other' }; |
| 115 | + result = getQueryConfigs(meta); |
| 116 | + expect(result).to.have.nested.property('0.collection', meta.collection); |
| 117 | + expect(result).to.have.nested.property('0.doc', meta.doc); |
| 118 | + }); |
| 119 | + |
| 120 | + it('with subcollections', () => { |
| 121 | + meta = { collection: 'test', doc: 'other', subcollections: [{ collection: 'thing' }] }; |
| 122 | + result = getQueryConfigs(meta); |
| 123 | + expect(result).to.have.nested.property('0.collection', meta.collection); |
| 124 | + expect(result).to.have.nested.property('0.doc', meta.doc); |
| 125 | + expect(result).to.have.nested.property('0.subcollections.0.collection', meta.subcollections[0].collection); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('firestoreRef', () => { |
| 131 | + beforeEach(() => { |
| 132 | + dispatch = sinon.spy(); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('doc', () => { |
| 136 | + it('creates ref', () => { |
| 137 | + meta = { collection: 'test', doc: 'other' }; |
| 138 | + const docSpy = sinon.spy(() => ({ })); |
| 139 | + const fakeFirebase = { firestore: () => ({ collection: () => ({ doc: docSpy }) }) }; |
| 140 | + result = firestoreRef(fakeFirebase, dispatch, meta); |
| 141 | + expect(result).to.be.an('object'); |
| 142 | + expect(docSpy).to.be.calledWith(meta.doc); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('subcollections', () => { |
| 147 | + it('creates ref with collection', () => { |
| 148 | + meta = { collection: 'test', doc: 'other', subcollections: [{ collection: 'thing' }] }; |
| 149 | + const docSpy = sinon.spy(() => ({ })); |
| 150 | + const fakeFirebase = { |
| 151 | + firestore: () => ({ |
| 152 | + collection: () => ({ |
| 153 | + doc: () => ({ |
| 154 | + collection: () => ({ doc: docSpy }), |
| 155 | + }), |
| 156 | + }), |
| 157 | + }), |
| 158 | + }; |
| 159 | + result = firestoreRef(fakeFirebase, dispatch, meta); |
| 160 | + expect(result).to.be.an('object'); |
| 161 | + // expect(docSpy).to.be.calledOnce(meta.subcollections[0].collection); |
| 162 | + }); |
| 163 | + |
| 164 | + it('creates ref with doc', () => { |
| 165 | + meta = { collection: 'test', doc: 'other', subcollections: [{ collection: 'thing', doc: 'again' }] }; |
| 166 | + const docSpy = sinon.spy(() => ({ })); |
| 167 | + const fakeFirebase = { |
| 168 | + firestore: () => ({ |
| 169 | + collection: () => ({ |
| 170 | + doc: () => ({ |
| 171 | + collection: () => ({ doc: docSpy }), |
| 172 | + }), |
| 173 | + }), |
| 174 | + }), |
| 175 | + }; |
| 176 | + result = firestoreRef(fakeFirebase, dispatch, meta); |
| 177 | + expect(result).to.be.an('object'); |
| 178 | + // expect(docSpy).to.be.calledWith(meta.subcollections[0].collection.doc); |
| 179 | + }); |
| 180 | + }); |
| 181 | + }); |
| 182 | +}); |
0 commit comments