Skip to content

Commit

Permalink
add more tests and refactor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenie committed Apr 11, 2018
1 parent 4ae22a9 commit 70159be
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
24 changes: 23 additions & 1 deletion __tests__/qselect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ describe('qselect', () => {
expect(node.innerHTML).toBe('t');
});

it('should get a null if selector exists and the context does not exist', () => {
it('should get an undefined if selector exists and the context does not exist', () => {
const node = $('span', 'input');
expect(node).toBeUndefined();
});

it('should get a null if selector does not exist and the context exists', () => {
const node = $('input', 'span');
expect(node).toBeNull();
});

it('should be a dom element if selector exists', () => {
const node = $('div');
expect(isDom(node)).toBeTruthy();
Expand All @@ -48,6 +53,12 @@ describe('qselect', () => {
expect(isDom(node)).toBeFalsy();
});

it('should throw if selector is not a dom string and context exists', () => {
expect(() => {
$(true, 'span');
}).toThrowError(errorSelector);
});

it('should throw if selector is Nan', () => {
expect(() => {
$(NaN);
Expand Down Expand Up @@ -148,6 +159,17 @@ describe('qselect', () => {
expect(node.length).toBeGreaterThanOrEqual(1);
});

it('should be undefined if selector is not a dom string and context exists', () => {
const node = $$(true, 'span');
expect(node).toBeUndefined();
});

it('should throw if selector exists and context is not a dom string', () => {
expect(() => {
$$('span', true);
}).toThrowError(errorSelector);
});

it('should throw if selector is Nan', () => {
expect(() => {
$$(NaN);
Expand Down
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const isSelectorString = (selector) => {
throw new Error('Selector must be a string');
};

const $ = (selector, context = undefined) => {
if (context) {
const root = document.querySelector(context);
const $ = (selector, rootNode = undefined) => {
if (rootNode) {
const root = document.querySelector(rootNode);

if (isDom(root)) {
return isSelectorString(selector) ? root.querySelector(selector) : undefined;
if (isDom(root) && isSelectorString(selector)) {
return root.querySelector(selector);
}
} else if (isSelectorString(selector)) {
return document.querySelector(selector);
Expand All @@ -23,12 +23,12 @@ const $ = (selector, context = undefined) => {
return undefined;
};

const $$ = (selector, context = undefined) => {
if (context) {
const $$ = (selector, rootNode = undefined) => {
if (rootNode) {
const root = document.querySelector(selector);

if (isDom(root)) {
return [...root.querySelectorAll(context)];
if (isDom(root) && isSelectorString(rootNode)) {
return [...root.querySelectorAll(rootNode)];
}
} else if (isSelectorString(selector)) {
return [...document.querySelectorAll(selector)];
Expand Down

0 comments on commit 70159be

Please sign in to comment.