cheerio-field.js 19.7 KB
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
/*eslint-env mocha*/
/*eslint no-invalid-this:0, no-undefined:0, max-len:[1, 150, 2], max-nested-callbacks:[1, 6]*/
/*jshint -W100*/
var assert = require('power-assert');
var typeOf = require('type-of');
var assign = require('object-assign');
var each   = require('foreach');
var helper = require('./_helper');
var cli    = require('../index');

describe('cheerio:field', function () {
  describe('根本的エラー', function () {
    it('form要素以外 => element is not form', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        try {
          $('div').eq(0).field('user');
          throw new Error('not thrown');
        } catch (e) {
          assert(e.message === 'element is not form');
        }
        done();
      });
    });

    it('要素数0 => no elements', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        try {
          $('header').field('user', 'hoge');
          throw new Error('not thrown');
        } catch (e) {
          assert(e.message === 'no elements');
        }
        done();
      });
    });

    it('nameが指定されているが文字列でない => name is not string or object', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        try {
          $('form[name=login]').field([ 1, 2, 3 ]);
          throw new Error('not thrown');
        } catch (e) {
          assert(e.message === 'name is not string or object');
        }
        done();
      });
    });
  });

  describe('nameのみ指定(設定値取得)', function () {
    it('nameのみ指定 => form配下のname部品の値を取得', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var actual = $('form[name=login]').field('user');
        assert(actual === 'guest');
        done();
      });
    });

    it('nameのみの指定でname部品がform内にない => undefined', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var actual = $('form[name=login]').field('user');
        assert(actual === 'guest');
        done();
      });
    });

    describe('select', function () {
      it('not multiple => 選択値の文字列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=select]');
          var expected = $form.find('select[name=single]').val();
          var actual = $form.field('single');
          assert(actual === expected);
          done();
        });
      });

      it('multiple(選択) => 選択値の配列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=select]');
          var expected = $form.find('select[name=multi]').val();
          var actual = $form.field('multi');
          assert.deepEqual(actual, expected);
          done();
        });
      });

      it('multiple(未選択) => 空配列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=select]');
          $form.find('select[name=multi]').val([]);
          var actual = $form.field('multi');
          assert(typeOf(actual) === 'array');
          assert(actual.length === 0);
          done();
        });
      });
    });

    describe('checkbox', function () {
      it('同名なし(選択) => 選択値の文字列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=checkbox]');
          var actual = $form.field('check1');
          assert(actual === '1');
          done();
        });
      });

      it('同名なし(未選択) => undefinedを返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=checkbox]');
          var actual = $form.field('check2');
          assert(typeOf(actual) === 'undefined');
          done();
        });
      });

      it('同名複数 => 選択値の配列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=checkbox]');
          $form.find('[name="check4[]"]').eq(1).tick();
          var actual = $form.field('check4[]');
          assert.deepEqual(actual, [ 'かきくけこ' ]);
          done();
        });
      });

      it('同名複数(未選択) => 空配列を返す', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=checkbox]');
          var actual = $form.field('check4[]');
          assert(typeOf(actual) === 'array');
          assert(actual.length === 0);
          done();
        });
      });
    });
  });

  describe('引数なし', function () {
    it('フォームの全部品とその状態を連想配列で返す', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=default-jp]');
        var expected = {
          text: 'あいうえお',
          checkbox: [ 'かきくけこ' ],
          radio: 'なにぬねの',
          select: [ 'ふふふふふ', 'ほほほほほ' ],
          textarea: 'まみむめも'
        };
        var actual = $form.field();
        assert.deepEqual(actual, expected);
        done();
      });
    });

    it('submit系要素 => 除外される', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=multi-submit]');
        var expected = {
          text: 'あいうえお',
          checkbox: [ 'bbb' ]
        };
        var actual = $form.field();
        assert.deepEqual(actual, expected);
        done();
      });
    });
  });

  describe('name,valueを指定(nameにvalueをセット)', function () {
    var types = {
      string: 'hey',
      array: [ 1, 2, 3 ],
      object: { x: 1, y: 2, z: 3 },
      number: 12,
      functino: function () {
        return 'hello';
      },
      date: new Date('2015/12/31 23:59:59'),
      regexp: /asdf/,
      null: null,
      undefined: undefined,
      true: true,
      false: false
    };
    each(types, function (val, type) {
      it('cheerioで直接セットしたのと同じ状態になる(' + type + ')', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=default-jp]');
          var $text = $form.find('input[name=text]');
          $text.val(val);
          var expected = $text.val();
          $form.field('text', val);
          var actual = $form.field('text');
          assert(actual === expected);
          done();
        });
      });
    });

    describe('複数値を受け付ける部品', function () {
      it('文字列valueをセット => name部品に配列[value]をセット', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=default-jp]');
          var expected = $form.field();
          var set = {
            checkbox: 'さしすせそ',
            select: 'ひひひひひ'
          };
          var enc = {};
          each(set, function (val, name) {
            expected[name] = [ val ];
            enc[name] = encodeURIComponent(val);
            $form.field(name, val);
          });
          assert.deepEqual($form.field(), expected);
          $form.submit(function (err, $, res, body) {
            var qp = helper.qsparse(res.headers['post-data']);
            each(set, function (val, name) {
              assert.deepEqual(qp[name], enc[name]);
            });
            done();
          });
        });
      });

      it('配列[value ...]をセット => 配列のままセットされる', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=default-jp]');
          var expected = $form.field();
          var set = {
            checkbox: [ 'かきくけこ', 'さしすせそ' ],
            select: [ 'ははははは', 'ひひひひひ' ]
          };
          var enc = {};
          each(set, function (val, name) {
            expected[name] = val;
            enc[name] = expected[name].map(function (v) {
              return encodeURIComponent(v);
            });
            $form.field(name, val);
          });
          assert.deepEqual($form.field(), expected);
          $form.submit(function (err, $, res, body) {
            var qp = helper.qsparse(res.headers['post-data']);
            each(set, function (val, name) {
              assert.deepEqual(qp[name], enc[name]);
            });
            done();
          });
        });
      });
    });

    describe('複数値を受け付けない部品', function () {
      it('配列[value ...]をセット => 文字列化されてセットされる', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=default-jp]');
          var expected = $form.field();
          var set = {
            text: [ 'アアアアア', 'イイイイイ' ],
            textarea: [ 'ウウウウウ', 'エエエエエ' ]
          };
          var enc = {};
          each(set, function (val, name) {
            expected[name] = String(val);
            enc[name] = encodeURIComponent(expected[name]);
            $form.field(name, val);
          });
          assert.deepEqual($form.field(), expected);
          $form.submit(function (err, $, res, body) {
            var qp = helper.qsparse(res.headers['post-data']);
            each(set, function (val, name) {
              assert.deepEqual(qp[name], enc[name]);
            });
            done();
          });
        });
      });
    });

    describe('checkbox', function () {
      it('valueが存在しない => 何も変更しない', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=checkbox]');
          var expected = $form.field();
          $form.field('check3', function () {
            return '33';
          });
          assert.deepEqual($form.field(), expected);
          done();
        });
      });

      it('valueが存在する => 選択状態にする(元の選択状態は解除される)', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=multi-submit]');
          var expected = $form.field();
          expected.checkbox = [ 'ccc' ];
          $form.field('checkbox', 'ccc');
          assert.deepEqual($form.field(), expected);
          assert($('input[name=checkbox][value=ccc]').attr('checked') === 'checked');
          done();
        });
      });
    });

    describe('radio', function () {
      it('valueが存在しない => 何も変更しない', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=radio]');
          var expected = $form.field();
          $form.field('radio1', 'aaa');
          assert.deepEqual($form.field(), expected);
          done();
        });
      });

      it('valueが存在する => 選択状態にする(元の選択状態は解除される)', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=radio]');
          var expected = $form.field();
          expected.radio1 = 'zzz';
          $form.field('radio1', 'zzz');
          assert.deepEqual($form.field(), expected);
          assert($('input[name=radio1][value=zzz]').attr('checked') === 'checked');
          done();
        });
      });

      it('valueが配列 => 文字列にキャストされる(配列内に該当するvalueがあっても無視される)', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
          var $form = $('form[name=radio]');
          var expected = $form.field();
          expected.radio1 = 'yyy';
          $form.field('radio1', [ 'xxx', 'yyy', 'zzz' ]);
          assert.deepEqual($form.field(), expected);
          done();
        });
      });
    });
  });

  describe('nameが存在しない(onNotFoundによって分岐)', function () {
    it('指定なし => valueはセットされない', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        var name = '__user';
        var expected = $form.field();
        $form.field(name, 'hoge');
        assert(name in $form.field() === false);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('append(value文字列) => 新規にname部品(hidden)が作成される', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        var name = '__user';
        var value = 'hoge';
        var expected = $form.field();
        expected[name] = value;
        $form.field(name, value, 'append');
        assert($form.field()[name] === value);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('append(value配列) => 新規にname部品(checkbox)が作成される', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        var name = '__user';
        var value = [ 'foo', 'bar', 'baz', 1, 2, 3 ];
        var expected = $form.field();
        expected[name] = value.map(function (v) {
          return String(v);
        });
        $form.field(name, value, 'append');
        assert.deepEqual($form.field()[name], expected[name]);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('appendした要素に値をセット => 反映される', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        var name = '__user';
        var expected = $form.field();
        expected[name] = 'ほげ';
        $form.field(name, 'hoge', 'append');
        $form.field(name, 'ほげ', 'append');
        assert.deepEqual($form.field()[name], expected[name]);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('throw => 例外発生', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        try {
          $form.field('__user', 'hoge', 'throw');
          throw new Error('not thrown');
        } catch (e) {
          assert(e.message === 'Element named "__user" could not be found in this form');
        }
        done();
      });
    });
  });

  describe('nameに連想配列を指定', function () {
    it('複数の値が一度に設定される', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=default-jp]');
        var set = {
          text: 'アイウエオ',
          checkbox: [ 'かきくけこ', 'さしすせそ' ],
          radio: 'たちつてと',
          select: [ 'ははははは', 'へへへへへ' ],
          textarea: 'マミムメモ'
        };
        var expected = assign($form.field(), set);
        $form.field(set);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('指定しない部品は変更されない', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=select]');
        var set = {
          single: '4'
        };
        var expected = assign($form.field(), set);
        $form.field(set);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    describe('連想配列のnameが存在しない(onNotFoundによって分岐)', function () {
      before(function () {
        this.set = {
          password: 'bar',
          message: 'hello world!',
          custom_string: 'foo',
          custom_array: [ 'aaa', 'bbb', 'ccc' ]
        };
      });

      it('指定なし => valueはセットされない', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), (function (err, $, res, body) {
          var $form = $('form[name=login]');
          var expected = $form.field();
          $form.field(this.set);
          var notfounds = [];
          each(this.set, function (val, name) {
            if (name in expected) {
              expected[name] = val;
            } else {
              notfounds.push(name);
            }
          });
          each(notfounds, function (val) {
            assert(typeOf($form.field('val')) === 'undefined');
          });
          assert.deepEqual($form.field(), expected);
          done();
        }).bind(this));
      });

      it('append => 新規にname部品(hidden/checkbox)が作成される', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), (function (err, $, res, body) {
          var $form = $('form[name=login]');
          $form.field(this.set, 'append');
          var expected = assign($form.field(), this.set);
          assert.deepEqual($form.field(), expected);
          assert($form.find('[name=custom_string]').attr('type') === 'hidden');
          assert($form.find('[name=custom_array]').attr('type') === 'checkbox');
          done();
        }).bind(this));
      });

      it('appendした要素に値をセット => 反映される', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), (function (err, $, res, body) {
          var $form = $('form[name=login]');
          $form.field(this.set, 'append');
          var set2 = {
            custom_string: 'んんんんん',
            custom_array: 'ccc'
          };
          var expected = assign($form.field(), this.set, set2);
          expected.custom_array = [ expected.custom_array ];
          $form.field(set2);
          assert.deepEqual($form.field(), expected);
          done();
        }).bind(this));
      });

      it('throw => 例外発生', function (done) {
        cli.fetch(helper.url('form', 'utf-8'), (function (err, $, res, body) {
          var $form = $('form[name=login]');
          try {
            $form.field(this.set, 'throw');
            throw new Error('not thrown');
          } catch (e) {
            assert(e.message === 'Element named "custom_string" could not be found in this form');
          }
          done();
        }).bind(this));
      });
    });
  });

  describe('値セット系はメソッドチェーン可能', function () {
    it('単一値', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var $form = $('form[name=login]');
        var expected = assign($form.field(), {
          user: 'foobar',
          password: 'hogefuga'
        });
        $form.field('user', expected.user).field('password', expected.password);
        assert.deepEqual($form.field(), expected);
        done();
      });
    });

    it('連想配列', function (done) {
      cli.fetch(helper.url('form', 'utf-8'), function (err, $, res, body) {
        var set1 = {
          password: 'bar',
          custom_string: 'foo'
        };
        var set2 = {
          message: 'hello world!',
          custom_array: [ 'aaa', 'bbb', 'ccc' ]
        };
        var $form = $('form[name=login]');
        var expected = assign($form.field(), set1, set2);
        delete expected.custom_string;
        $form.field(set1).field(set2, 'append');
        assert.deepEqual($form.field(), expected);
        done();
      });
    });
  });
});