|
| 1 | +import test from 'ava' |
| 2 | +import { allowedUndefinedStubProps, makeStub } from '../../src/util/testing-utils' |
| 3 | + |
| 4 | +test('make a stub', async (t) => { |
| 5 | + const stub = makeStub('stub', { |
| 6 | + name: 'stub-name', |
| 7 | + count: 5, |
| 8 | + }) |
| 9 | + |
| 10 | + t.is(stub.name, 'stub-name') |
| 11 | + t.is(stub.count, 5) |
| 12 | +}) |
| 13 | + |
| 14 | +test('make a stub with nested fields', async (t) => { |
| 15 | + const stub = makeStub('stub', { |
| 16 | + name: 'stub-name', |
| 17 | + nested: { |
| 18 | + count: 5, |
| 19 | + }, |
| 20 | + }) |
| 21 | + |
| 22 | + t.is(stub.name, 'stub-name') |
| 23 | + t.is(stub.nested.count, 5) |
| 24 | +}) |
| 25 | + |
| 26 | +test('accessing an absent field should throw an error', async (t) => { |
| 27 | + const stub = makeStub('stub', { |
| 28 | + name: 'stub-name', |
| 29 | + nested: { |
| 30 | + count: 5, |
| 31 | + }, |
| 32 | + }) |
| 33 | + |
| 34 | + t.throws( |
| 35 | + () => { |
| 36 | + // @ts-expect-error intended |
| 37 | + t.is(stub.count, undefined) |
| 38 | + }, |
| 39 | + { |
| 40 | + message: "Property 'stub.count' does not exist", |
| 41 | + }, |
| 42 | + ) |
| 43 | +}) |
| 44 | + |
| 45 | +test('accessing a nested absent field should throw an error', async (t) => { |
| 46 | + const stub = makeStub('stub', { |
| 47 | + name: 'stub-name', |
| 48 | + nested: { |
| 49 | + count: 5, |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + t.throws( |
| 54 | + () => { |
| 55 | + // @ts-expect-error intended |
| 56 | + t.is(stub.nested.name, undefined) |
| 57 | + }, |
| 58 | + { |
| 59 | + message: "Property 'stub.nested.name' does not exist", |
| 60 | + }, |
| 61 | + ) |
| 62 | +}) |
| 63 | + |
| 64 | +test('fields used by jest are allowed to be undefined', async (t) => { |
| 65 | + const stub = makeStub('stub', { |
| 66 | + name: 'stub-name', |
| 67 | + count: 5, |
| 68 | + }) |
| 69 | + |
| 70 | + // @ts-expect-error intended |
| 71 | + t.is(stub.nodeType, undefined) |
| 72 | + // @ts-expect-error intended |
| 73 | + t.is(stub.tagName, undefined) |
| 74 | +}) |
| 75 | + |
| 76 | +test('Symbol props are allowed to be undefined', async (t) => { |
| 77 | + const stub = makeStub('stub', { |
| 78 | + name: 'stub-name', |
| 79 | + count: 5, |
| 80 | + }) |
| 81 | + |
| 82 | + // @ts-expect-error intended |
| 83 | + t.is(stub[Symbol('my symbol')], undefined) |
| 84 | +}) |
| 85 | + |
| 86 | +test('allowedUndefinedStubProps can be extended and restored', async (t) => { |
| 87 | + const customProp = 'myCustomProp' |
| 88 | + |
| 89 | + const stub = makeStub('stub', { |
| 90 | + name: 'stub-name', |
| 91 | + count: 5, |
| 92 | + }) |
| 93 | + |
| 94 | + t.throws( |
| 95 | + () => { |
| 96 | + // @ts-expect-error intended |
| 97 | + t.is(stub[customProp], undefined) |
| 98 | + }, |
| 99 | + { |
| 100 | + message: "Property 'stub.myCustomProp' does not exist", |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + allowedUndefinedStubProps.push('myCustomProp') |
| 105 | + |
| 106 | + // @ts-expect-error intended |
| 107 | + t.is(stub[customProp], undefined) |
| 108 | + |
| 109 | + allowedUndefinedStubProps.pop() |
| 110 | + |
| 111 | + t.throws( |
| 112 | + () => { |
| 113 | + // @ts-expect-error intended |
| 114 | + t.is(stub[customProp], undefined) |
| 115 | + }, |
| 116 | + { |
| 117 | + message: "Property 'stub.myCustomProp' does not exist", |
| 118 | + }, |
| 119 | + ) |
| 120 | +}) |
0 commit comments