Commit 95265c1c authored by Robert Knight's avatar Robert Knight

Throw an error if two selectors share the same name

Since selectors are attached to the `store` as methods, selectors from
different modules are not allowed to have the same name.
parent a8fe2c88
......@@ -154,5 +154,37 @@ describe('sidebar/store/util', function () {
assert.equal(bound.rootCountAnnotations1(), 1);
assert.equal(bound.rootCountAnnotations2(), 1);
});
it('throws an error if selector names in different modules conflict', () => {
const getState = () => ({});
assert.throws(() => {
util.bindSelectors(
{
moduleA: {
selectors: { someSelector: () => {} },
},
moduleB: {
selectors: { someSelector: () => {} },
},
},
getState
);
}, 'Duplicate selector "someSelector"');
});
it('throws an error if selector names in different modules conflict', () => {
const getState = () => ({});
assert.throws(() => {
util.bindSelectors(
{
moduleA: {
selectors: { someSelector: () => {} },
rootSelectors: { someSelector: () => {} },
},
},
getState
);
});
});
});
});
......@@ -49,11 +49,17 @@ export function bindSelectors(namespaces, getState) {
const { selectors, rootSelectors = {} } = namespaces[namespace];
Object.keys(selectors).forEach(selector => {
if (boundSelectors[selector]) {
throw new Error(`Duplicate selector "${selector}"`);
}
boundSelectors[selector] = (...args) =>
selectors[selector](getState()[namespace], ...args);
});
Object.keys(rootSelectors).forEach(selector => {
if (boundSelectors[selector]) {
throw new Error(`Duplicate selector "${selector}"`);
}
boundSelectors[selector] = (...args) =>
rootSelectors[selector](getState(), ...args);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment