Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenie committed Apr 10, 2018
1 parent b665f4a commit 14a6e62
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ stages:

jobs:
include:
if: type IN (push, pull_request)
- stage: "test"
80 changes: 78 additions & 2 deletions __tests__/qselect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('qselect', () => {
it('should throw if selector is Nan', () => {
expect(() => {
$(NaN);
}).toThrowError();
}).toThrowError(errorSelector);
});

it('should throw if selector is Infinity', () => {
Expand Down Expand Up @@ -133,9 +133,85 @@ describe('qselect', () => {
expect(node).toHaveLength(0);
});

it('should be an empty array if selector exists and context does not exist', () => {
const node = $$('.lol', 'input');
expect(node).toHaveLength(0);
});

it('should get an array of dom nodes if selector exists', () => {
const node = $$('div');
expect(node.length).toBeGreaterThan(1);
expect(node.length).toBeGreaterThanOrEqual(1);
});

it('should get an array of dom nodes if selector exists and context exists', () => {
const node = $$('.lol', 'span');
expect(node.length).toBeGreaterThanOrEqual(1);
});

it('should throw if selector is Nan', () => {
expect(() => {
$$(NaN);
}).toThrowError(errorSelector);
});

it('should throw if selector is Infinity', () => {
expect(() => {
$$(Infinity);
}).toThrowError(errorSelector);
});

it('should throw if selector is a Boolean', () => {
expect(() => {
$$(Boolean);
}).toThrowError(errorSelector);
});

it('should throw if selector is an empty Object', () => {
expect(() => {
$$({});
}).toThrowError(errorSelector);
});

it('should throw if selector is a RegExp', () => {
expect(() => {
$$(new RegExp());
}).toThrowError(errorSelector);
});

it('should throw if selector is a Date', () => {
expect(() => {
$$(new Date());
}).toThrowError(errorSelector);
});

it('should throw if selector is an Array', () => {
expect(() => {
$$([]);
}).toThrowError(errorSelector);
});

it('should throw if selector is an Integer', () => {
expect(() => {
$$(1);
}).toThrowError(errorSelector);
});

it('should throw if selector is undefined', () => {
expect(() => {
$$(undefined);
}).toThrowError(errorSelector);
});

it('should throw if selector is null', () => {
expect(() => {
$$(null);
}).toThrowError(errorSelector);
});

it('should throw if selector is an empty string', () => {
expect(() => {
$$('');
}).toThrowError(invalidSelector);
});
});
});
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const $ = (selector, context = undefined) => {

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

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

0 comments on commit 14a6e62

Please sign in to comment.