From 14a6e628d29b613fdf8dee6561f0fc0ba28cc27b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucien=20B=C3=A9ni=C3=A9?= Date: Tue, 10 Apr 2018 10:55:06 -0400 Subject: [PATCH] add more tests --- .travis.yml | 1 + __tests__/qselect.test.js | 80 ++++++++++++++++++++++++++++++++++++++- index.js | 4 +- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index e0b5cfc..c0ab8d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,4 +17,5 @@ stages: jobs: include: + if: type IN (push, pull_request) - stage: "test" diff --git a/__tests__/qselect.test.js b/__tests__/qselect.test.js index ddcdb63..d85c69f 100644 --- a/__tests__/qselect.test.js +++ b/__tests__/qselect.test.js @@ -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', () => { @@ -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); }); }); }); diff --git a/index.js b/index.js index 1b76901..4cfd618 100644 --- a/index.js +++ b/index.js @@ -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)];