File size: 4,178 Bytes
a8063bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import { describe, it, expect, beforeEach } from 'vitest';
import { type CheerioAPI } from '../index.js';
import { cheerio, forms } from '../__fixtures__/fixtures.js';
describe('$(...)', () => {
let $: CheerioAPI;
beforeEach(() => {
$ = cheerio.load(forms);
});
describe('.serializeArray', () => {
it('() : should get form controls', () => {
expect($('form#simple').serializeArray()).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
]);
});
it('() : should get nested form controls', () => {
expect($('form#nested').serializeArray()).toHaveLength(2);
const data = $('form#nested').serializeArray();
data.sort((a, b) => (a.value > b.value ? 1 : -1));
expect(data).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
{
name: 'vegetable',
value: 'Carrot',
},
]);
});
it('() : should not get disabled form controls', () => {
expect($('form#disabled').serializeArray()).toStrictEqual([]);
});
it('() : should not get form controls with the wrong type', () => {
expect($('form#submit').serializeArray()).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
]);
});
it('() : should get selected options', () => {
expect($('form#select').serializeArray()).toStrictEqual([
{
name: 'fruit',
value: 'Orange',
},
]);
});
it('() : should not get unnamed form controls', () => {
expect($('form#unnamed').serializeArray()).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
]);
});
it('() : should get multiple selected options', () => {
expect($('form#multiple').serializeArray()).toHaveLength(2);
const data = $('form#multiple').serializeArray();
data.sort((a, b) => (a.value > b.value ? 1 : -1));
expect(data).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
{
name: 'fruit',
value: 'Orange',
},
]);
});
it('() : should get individually selected elements', () => {
const data = $('form#nested input').serializeArray();
data.sort((a, b) => (a.value > b.value ? 1 : -1));
expect(data).toStrictEqual([
{
name: 'fruit',
value: 'Apple',
},
{
name: 'vegetable',
value: 'Carrot',
},
]);
});
it('() : should standardize line breaks', () => {
expect($('form#textarea').serializeArray()).toStrictEqual([
{
name: 'fruits',
value: 'Apple\r\nOrange',
},
]);
});
it("() : shouldn't serialize the empty string", () => {
expect($('<input value=pineapple>').serializeArray()).toStrictEqual([]);
expect(
$('<input name="" value=pineapple>').serializeArray(),
).toStrictEqual([]);
expect(
$('<input name="fruit" value=pineapple>').serializeArray(),
).toStrictEqual([
{
name: 'fruit',
value: 'pineapple',
},
]);
});
it('() : should serialize inputs without value attributes', () => {
expect($('<input name="fruit">').serializeArray()).toStrictEqual([
{
name: 'fruit',
value: '',
},
]);
});
});
describe('.serialize', () => {
it('() : should get form controls', () => {
expect($('form#simple').serialize()).toBe('fruit=Apple');
});
it('() : should get nested form controls', () => {
expect($('form#nested').serialize()).toBe('fruit=Apple&vegetable=Carrot');
});
it('() : should not get disabled form controls', () => {
expect($('form#disabled').serialize()).toBe('');
});
it('() : should get multiple selected options', () => {
expect($('form#multiple').serialize()).toBe('fruit=Apple&fruit=Orange');
});
it("() : should encode spaces as +'s", () => {
expect($('form#spaces').serialize()).toBe('fruit=Blood+orange');
});
});
});
|