|
1 |
| -const expect = require('chai').expect; |
| 1 | +const app = require('hadron-app'); |
| 2 | +const AppRegistry = require('hadron-app-registry'); |
| 3 | +const { expect } = require('chai'); |
| 4 | +const Reflux = require('reflux'); |
2 | 5 | const NamespaceStore = require('../../src/internal-packages/app/lib/stores/namespace-store');
|
3 | 6 |
|
| 7 | +/** |
| 8 | + * Useful background information on namespaces: |
| 9 | + * https://docs.mongodb.com/manual/reference/limits/#naming-restrictions |
| 10 | + */ |
| 11 | +describe('NamespaceStore', () => { |
| 12 | + const initialDatabase = 'foo'; |
| 13 | + const initialCollection = 'bar.baz'; |
| 14 | + let appRegistry; |
4 | 15 |
|
5 |
| -describe('NamespaceStore', function() { |
6 |
| - describe('#set ns', function() { |
7 |
| - it('triggers a store event', function(done) { |
8 |
| - var unsubscribe = NamespaceStore.listen(function(ns) { |
| 16 | + beforeEach(() => { |
| 17 | + NamespaceStore.ns = `${initialDatabase}.${initialCollection}`; |
| 18 | + appRegistry = app.appRegistry; |
| 19 | + app.appRegistry = new AppRegistry(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + app.appRegistry = appRegistry; |
| 24 | + }); |
| 25 | + |
| 26 | + describe('#set ns', () => { |
| 27 | + it('triggers a store event', (done) => { |
| 28 | + const unsubscribe = NamespaceStore.listen((ns) => { |
9 | 29 | expect(ns).to.equal('database.collection');
|
10 | 30 | unsubscribe();
|
11 | 31 | done();
|
12 | 32 | });
|
13 | 33 | NamespaceStore.ns = 'database.collection';
|
14 | 34 | });
|
| 35 | + |
| 36 | + context('when collection changes', () => { |
| 37 | + it('calls onCollectionChanged', (done) => { |
| 38 | + const newNamespace = `${initialDatabase}.change.the-collection.please`; |
| 39 | + const CollectionSubscriberStore = Reflux.createStore({ |
| 40 | + onCollectionChanged(namespace) { |
| 41 | + expect(namespace).to.be.equal(newNamespace); |
| 42 | + done(); |
| 43 | + } |
| 44 | + }); |
| 45 | + app.appRegistry.registerStore('CollectionSubscriber.Store', CollectionSubscriberStore); |
| 46 | + NamespaceStore.ns = newNamespace; |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + context('when the initial collection contains a dot', () => { |
| 51 | + context('when only the part after the dot changes', () => { |
| 52 | + it('calls onCollectionChanged', (done) => { |
| 53 | + NamespaceStore.ns = 'foo.bar.baz'; |
| 54 | + const newNamespace = 'foo.bar.jaguar'; |
| 55 | + const CollectionSubscriberStore = Reflux.createStore({ |
| 56 | + onCollectionChanged(namespace) { |
| 57 | + expect(namespace).to.be.equal(newNamespace); |
| 58 | + done(); |
| 59 | + } |
| 60 | + }); |
| 61 | + app.appRegistry.registerStore('CollectionSubscriber.Store', CollectionSubscriberStore); |
| 62 | + NamespaceStore.ns = newNamespace; |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + context('when the initial collection does not contain a dot', () => { |
| 68 | + it('calls onCollectionChanged', (done) => { |
| 69 | + NamespaceStore.ns = 'foo.bar'; |
| 70 | + const newNamespace = 'jaguar.bar'; |
| 71 | + const CollectionSubscriberStore = Reflux.createStore({ |
| 72 | + onCollectionChanged(namespace) { |
| 73 | + expect(namespace).to.be.equal(newNamespace); |
| 74 | + done(); |
| 75 | + } |
| 76 | + }); |
| 77 | + app.appRegistry.registerStore('CollectionSubscriber.Store', CollectionSubscriberStore); |
| 78 | + NamespaceStore.ns = newNamespace; |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + context('when database changes', () => { |
| 83 | + it('calls onDatabaseChanged', (done) => { |
| 84 | + const newNamespace = `changeTheDB.${initialCollection}`; |
| 85 | + const DatabaseSubscriberStore = Reflux.createStore({ |
| 86 | + onDatabaseChanged(namespace) { |
| 87 | + expect(namespace).to.be.equal(newNamespace); |
| 88 | + done(); |
| 89 | + } |
| 90 | + }); |
| 91 | + app.appRegistry.registerStore('DatabaseSubscriber.Store', DatabaseSubscriberStore); |
| 92 | + NamespaceStore.ns = newNamespace; |
| 93 | + }); |
| 94 | + }); |
15 | 95 | });
|
16 | 96 | });
|
0 commit comments