Skip to content

Commit

Permalink
test: add some tests (#1653)
Browse files Browse the repository at this point in the history
* updated some tests

* make test safer and more meaningful

Co-authored-by: 5saviahv <5saviahv@users.noreply.github.com>
  • Loading branch information
5saviahv and 5saviahv committed Jan 5, 2021
1 parent 5639f95 commit 1ebe05a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
18 changes: 16 additions & 2 deletions test/api/attributes.js
Expand Up @@ -175,8 +175,9 @@ describe('$(...)', function () {
});

it('(invalid key) : invalid prop should get undefined', function () {
var attr = checkbox.prop('lol');
expect(attr).toBe(undefined);
expect(checkbox.prop('lol')).toBe(undefined);
expect(checkbox.prop(4)).toBe(undefined);
expect(checkbox.prop(true)).toBe(undefined);
});

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

it('(key, value) : should update namespace', function () {
var imgs = $('<img>\n\n<img>\n\n<img>');
var nsHtml = 'http://www.w3.org/1999/xhtml';
imgs.prop('src', '#').prop('namespace', nsHtml);
expect(imgs.prop('namespace')).toBe(nsHtml);
imgs.prop('attribs', null);
expect(imgs.prop('src')).toBe(undefined);
});

it('(map) : object map should set multiple props', function () {
checkbox.prop({
id: 'check',
Expand Down Expand Up @@ -282,6 +292,7 @@ describe('$(...)', function () {
it('() : no data attribute should return an empty object', function () {
var data = $('.cailler').data();
expect(Object.keys(data)).toHaveLength(0);
expect($('.free').data()).toBe(undefined);
});

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

expect(function () {
a.data(4, 'throw');
}).not.toThrow();
expect(a.data('balls')).toStrictEqual('giandor');
expect(b.data('snack')).toStrictEqual('chocoletti');
});
Expand Down
40 changes: 40 additions & 0 deletions test/cheerio.js
@@ -1,6 +1,7 @@
'use strict';
var htmlparser2 = require('htmlparser2');
var cheerio = require('..');
var utils = require('../lib/utils');
var fixtures = require('./__fixtures__/fixtures');
var fruits = fixtures.fruits;
var food = fixtures.food;
Expand Down Expand Up @@ -64,16 +65,19 @@ describe('cheerio', function () {
expect($apple.childNodes[0].data).toBe('Apple');
}

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

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

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

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

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

// eslint-disable-next-line jest/expect-expect
it('should be able to select by attributes', function () {
var $apple = cheerio('li[class=apple]', fruits);
testAppleSelect($apple);
Expand Down Expand Up @@ -369,4 +376,37 @@ describe('cheerio', function () {
});
});
});
describe('util functions', function () {
it('camelCase function test', function () {
expect(utils.camelCase('cheerio.js')).toBe('cheerioJs');
expect(utils.camelCase('camel-case-')).toBe('camelCase');
expect(utils.camelCase('__directory__')).toBe('_directory_');
expect(utils.camelCase('_one-two.three')).toBe('OneTwoThree');
});

it('cssCase function test', function () {
expect(utils.cssCase('camelCase')).toBe('camel-case');
expect(utils.cssCase('jQuery')).toBe('j-query');
expect(utils.cssCase('neverSayNever')).toBe('never-say-never');
expect(utils.cssCase('CSSCase')).toBe('-c-s-s-case');
});

it('cloneDom : should be able clone single Elements', function () {
var main = cheerio('<p>Cheerio</p>');
var result = [];
utils.domEach(main, function (i, el) {
result = result.concat(utils.cloneDom(el));
});
expect(result).toHaveLength(1);
expect(result[0]).not.toBe(main[0]);
expect(main[0].children.length).toBe(result[0].children.length);
expect(cheerio(result).text()).toBe(main.text());
});

it('isHtml function test', function () {
expect(utils.isHtml('<html>')).toBe(true);
expect(utils.isHtml('\n<html>\n')).toBe(true);
expect(utils.isHtml('#main')).toBe(false);
});
});
});

0 comments on commit 1ebe05a

Please sign in to comment.