summaryrefslogtreecommitdiffstats
path: root/test/webidl/helpers.js
blob: f44d501323a91419cfcd72367cdc553fd2b69313 (plain)
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
'use strict'

const { test } = require('tap')
const { webidl } = require('../../lib/fetch/webidl')

test('webidl.interfaceConverter', (t) => {
  class A {}
  class B {}

  const converter = webidl.interfaceConverter(A)

  t.throws(() => {
    converter(new B())
  }, TypeError)

  t.doesNotThrow(() => {
    converter(new A())
  })

  t.end()
})

test('webidl.dictionaryConverter', (t) => {
  t.test('extraneous keys are provided', (t) => {
    const converter = webidl.dictionaryConverter([
      {
        key: 'key',
        converter: webidl.converters.USVString,
        defaultValue: 420,
        required: true
      }
    ])

    t.same(
      converter({
        a: 'b',
        key: 'string',
        c: 'd',
        get value () {
          return 6
        }
      }),
      { key: 'string' }
    )

    t.end()
  })

  t.test('defaultValue with key = null', (t) => {
    const converter = webidl.dictionaryConverter([
      {
        key: 'key',
        converter: webidl.converters['unsigned short'],
        defaultValue: 200
      }
    ])

    t.same(converter({ key: null }), { key: 0 })
    t.end()
  })

  t.test('no defaultValue and optional', (t) => {
    const converter = webidl.dictionaryConverter([
      {
        key: 'key',
        converter: webidl.converters.ByteString
      }
    ])

    t.same(converter({ a: 'b', c: 'd' }), {})
    t.end()
  })

  t.end()
})