diff --git a/test/api/attributes.js b/test/api/attributes.js index ec102e5d91..7f7a085fcb 100644 --- a/test/api/attributes.js +++ b/test/api/attributes.js @@ -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 () { @@ -198,6 +199,15 @@ describe('$(...)', function () { expect(checkbox.attr('checked')).toBe('checked'); }); + it('(key, value) : should update namespace', function () { + var imgs = $('\n\n\n\n'); + 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', @@ -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 () { @@ -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'); }); diff --git a/test/cheerio.js b/test/cheerio.js index 674ea57f8a..f38eb00e73 100644 --- a/test/cheerio.js +++ b/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; @@ -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); @@ -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); @@ -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('

Cheerio

'); + 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('')).toBe(true); + expect(utils.isHtml('\n\n')).toBe(true); + expect(utils.isHtml('#main')).toBe(false); + }); + }); });