Skip to content

Commit 1ebe05a

Browse files
authored
test: add some tests (#1653)
* updated some tests * make test safer and more meaningful Co-authored-by: 5saviahv <5saviahv@users.noreply.github.com>
1 parent 5639f95 commit 1ebe05a

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

test/api/attributes.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ describe('$(...)', function () {
175175
});
176176

177177
it('(invalid key) : invalid prop should get undefined', function () {
178-
var attr = checkbox.prop('lol');
179-
expect(attr).toBe(undefined);
178+
expect(checkbox.prop('lol')).toBe(undefined);
179+
expect(checkbox.prop(4)).toBe(undefined);
180+
expect(checkbox.prop(true)).toBe(undefined);
180181
});
181182

182183
it('(key, value) : should set prop', function () {
@@ -198,6 +199,15 @@ describe('$(...)', function () {
198199
expect(checkbox.attr('checked')).toBe('checked');
199200
});
200201

202+
it('(key, value) : should update namespace', function () {
203+
var imgs = $('<img>\n\n<img>\n\n<img>');
204+
var nsHtml = 'http://www.w3.org/1999/xhtml';
205+
imgs.prop('src', '#').prop('namespace', nsHtml);
206+
expect(imgs.prop('namespace')).toBe(nsHtml);
207+
imgs.prop('attribs', null);
208+
expect(imgs.prop('src')).toBe(undefined);
209+
});
210+
201211
it('(map) : object map should set multiple props', function () {
202212
checkbox.prop({
203213
id: 'check',
@@ -282,6 +292,7 @@ describe('$(...)', function () {
282292
it('() : no data attribute should return an empty object', function () {
283293
var data = $('.cailler').data();
284294
expect(Object.keys(data)).toHaveLength(0);
295+
expect($('.free').data()).toBe(undefined);
285296
});
286297

287298
it('(invalid key) : invalid data attribute should return `undefined`', function () {
@@ -361,6 +372,9 @@ describe('$(...)', function () {
361372
// Adding as string.
362373
var b = $('.linth').data('snack', 'chocoletti');
363374

375+
expect(function () {
376+
a.data(4, 'throw');
377+
}).not.toThrow();
364378
expect(a.data('balls')).toStrictEqual('giandor');
365379
expect(b.data('snack')).toStrictEqual('chocoletti');
366380
});

test/cheerio.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var htmlparser2 = require('htmlparser2');
33
var cheerio = require('..');
4+
var utils = require('../lib/utils');
45
var fixtures = require('./__fixtures__/fixtures');
56
var fruits = fixtures.fruits;
67
var food = fixtures.food;
@@ -64,16 +65,19 @@ describe('cheerio', function () {
6465
expect($apple.childNodes[0].data).toBe('Apple');
6566
}
6667

68+
// eslint-disable-next-line jest/expect-expect
6769
it('should be able to select .apple with only a context', function () {
6870
var $apple = cheerio('.apple', fruits);
6971
testAppleSelect($apple);
7072
});
7173

74+
// eslint-disable-next-line jest/expect-expect
7275
it('should be able to select .apple with a node as context', function () {
7376
var $apple = cheerio('.apple', cheerio(fruits)[0]);
7477
testAppleSelect($apple);
7578
});
7679

80+
// eslint-disable-next-line jest/expect-expect
7781
it('should be able to select .apple with only a root', function () {
7882
var $apple = cheerio('.apple', null, fruits);
7983
testAppleSelect($apple);
@@ -118,16 +122,19 @@ describe('cheerio', function () {
118122
});
119123
});
120124

125+
// eslint-disable-next-line jest/expect-expect
121126
it('should be able to do: cheerio("#fruits .apple")', function () {
122127
var $apple = cheerio('#fruits .apple', fruits);
123128
testAppleSelect($apple);
124129
});
125130

131+
// eslint-disable-next-line jest/expect-expect
126132
it('should be able to do: cheerio("li.apple")', function () {
127133
var $apple = cheerio('li.apple', fruits);
128134
testAppleSelect($apple);
129135
});
130136

137+
// eslint-disable-next-line jest/expect-expect
131138
it('should be able to select by attributes', function () {
132139
var $apple = cheerio('li[class=apple]', fruits);
133140
testAppleSelect($apple);
@@ -369,4 +376,37 @@ describe('cheerio', function () {
369376
});
370377
});
371378
});
379+
describe('util functions', function () {
380+
it('camelCase function test', function () {
381+
expect(utils.camelCase('cheerio.js')).toBe('cheerioJs');
382+
expect(utils.camelCase('camel-case-')).toBe('camelCase');
383+
expect(utils.camelCase('__directory__')).toBe('_directory_');
384+
expect(utils.camelCase('_one-two.three')).toBe('OneTwoThree');
385+
});
386+
387+
it('cssCase function test', function () {
388+
expect(utils.cssCase('camelCase')).toBe('camel-case');
389+
expect(utils.cssCase('jQuery')).toBe('j-query');
390+
expect(utils.cssCase('neverSayNever')).toBe('never-say-never');
391+
expect(utils.cssCase('CSSCase')).toBe('-c-s-s-case');
392+
});
393+
394+
it('cloneDom : should be able clone single Elements', function () {
395+
var main = cheerio('<p>Cheerio</p>');
396+
var result = [];
397+
utils.domEach(main, function (i, el) {
398+
result = result.concat(utils.cloneDom(el));
399+
});
400+
expect(result).toHaveLength(1);
401+
expect(result[0]).not.toBe(main[0]);
402+
expect(main[0].children.length).toBe(result[0].children.length);
403+
expect(cheerio(result).text()).toBe(main.text());
404+
});
405+
406+
it('isHtml function test', function () {
407+
expect(utils.isHtml('<html>')).toBe(true);
408+
expect(utils.isHtml('\n<html>\n')).toBe(true);
409+
expect(utils.isHtml('#main')).toBe(false);
410+
});
411+
});
372412
});

0 commit comments

Comments
 (0)