Skip to content

Commit

Permalink
fix(getContainer): container can be null (#27)
Browse files Browse the repository at this point in the history
* fix(getContainer): container can be null
* test(getContainer): add test cases for getContainer
  • Loading branch information
SevenOutman committed Dec 16, 2021
1 parent e972410 commit 121ac6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/getContainer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default (container: Element | (() => Element), defaultContainer?: Element): Element => {
export default (
container: Element | null | (() => Element | null),
defaultContainer?: Element
): Element => {
container = typeof container === 'function' ? container() : container;
return container || defaultContainer;
};
26 changes: 26 additions & 0 deletions test/querySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,30 @@ describe('Query', () => {
expect(lib.isFocusable(createElement('div', { tabIndex: -1 }))).to.equal(true);
});
});

describe('getContainer', () => {
it('Should return the container if present', () => {
const container = {};

expect(lib.getContainer(container)).to.equal(container);
});

it('Should return the return value of container when container is a function', () => {
const container = {};

expect(lib.getContainer(() => container)).to.equal(container);
});

it('Should return defaultContainer if container is null', () => {
const defaultContainer = {};

expect(lib.getContainer(null, defaultContainer)).to.equal(defaultContainer);
});

it('Should return defaultContainer if container returns null', () => {
const defaultContainer = {};

expect(lib.getContainer(() => null, defaultContainer)).to.equal(defaultContainer);
});
});
});

0 comments on commit 121ac6d

Please sign in to comment.