HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcode/frontend/node_modules/custom-select/src/test/insert-before.js
import test from 'tape';
import customSelect from './../';

let option;
let actual;
let expected;
let target;

document.body.innerHTML = '';
const select = document.createElement('select');
select.innerHTML = `
  <option value="">Select...</option>
  <optgroup label="Moto">
    <option value="suzuki">Suzuki</option>
  </optgroup>
  <optgroup label="Auto">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>`;
document.body.appendChild(select);
const customselect = customSelect('select')[0];

test('Insert an option before an optgroup', assert => {
  target = select.children[1]; // Moto optgroup
  option = document.createElement('option');
  option.value = 'mustang';
  option.text = 'Mustang';

  customselect.insertBefore(option, target);

  actual = select.children[1].value;
  expected = 'mustang';
  assert.deepEqual(actual, expected,
    'should return mustang');
  assert.end();
});

test('Insert an option before an option in a optgroup', assert => {
  target = select.options[2]; // Mercedes
  option = document.createElement('option');
  option.value = 'subaru';
  option.text = 'Subaru';

  customselect.insertBefore(option, target);

  // The new injected custom option
  actual = select.parentNode.children[2].children[2].children[0].dataset.value;
  expected = 'subaru';
  assert.deepEqual(actual, expected,
    'should return subaru');
  assert.end();
});

test('Insert an optgroup with an option before an option', assert => {
  target = select.children[1]; // Moto optgroup

  const optgroup = document.createElement('optgroup');
  optgroup.setAttribute('label', 'Bike');

  option = document.createElement('option');
  option.value = 'mountain bike';
  option.text = 'Mountain bike';

  customselect.insertBefore(optgroup, target);

  // The new injected optgroup
  actual = select.children[1].getAttribute('label');
  expected = 'Bike';
  assert.deepEqual(actual, expected,
    'should return Bike');
  assert.end();
});

test('InsertBefore: Use a string as the target parameter', assert => {
  option = document.createElement('option');
  assert.throws(() => { select.parentNode.customSelect.insertBefore(option, 'a string'); },
   TypeError, 'should throw TypeError');
  assert.end();
});

test('InsertBefore: Use undefined as the target parameter', assert => {
  assert.throws(() => { select.parentNode.customSelect.insertBefore(option); }, TypeError,
    'should throw TypeError');
  assert.end();
});

test('InsertBefore: Use an invalid HTMLElement target parameter', assert => {
  assert.throws(() => { select.parentNode.customSelect.insertBefore(option, option); }, TypeError,
    'should throw TypeError');
  assert.end();
});

test('InsertBefore: Use undefined as the node parameter', assert => {
  assert.throws(() => { select.parentNode.customSelect.insertBefore(undefined, target); },
    TypeError, 'should throw TypeError');
  assert.end();
});