|
1 |
| -import assert from 'assert' |
| 1 | +import assert, { CallTracker } from 'assert' |
2 | 2 | import fs from 'fs'
|
3 | 3 | import { pick } from 'lodash'
|
4 | 4 | import { basename, extname, resolve } from 'path'
|
5 |
| -import { assert as assertValue, create as createValue, StructError } from '..' |
| 5 | +import { |
| 6 | + any, |
| 7 | + assert as assertValue, |
| 8 | + Context, |
| 9 | + create as createValue, |
| 10 | + deprecated, |
| 11 | + StructError, |
| 12 | +} from '..' |
6 | 13 |
|
7 | 14 | describe('superstruct', () => {
|
8 | 15 | describe('api', () => {
|
@@ -83,10 +90,40 @@ describe('superstruct', () => {
|
83 | 90 | })
|
84 | 91 | }
|
85 | 92 | })
|
| 93 | + |
| 94 | + describe('deprecated', () => { |
| 95 | + it('does not log deprecated type if value is undefined', () => { |
| 96 | + const tracker = new CallTracker() |
| 97 | + const logSpy = buildSpyWithZeroCalls(tracker) |
| 98 | + assertValue(undefined, deprecated(any(), logSpy)) |
| 99 | + tracker.verify() |
| 100 | + }) |
| 101 | + |
| 102 | + it('logs deprecated type to passed function if value is present', () => { |
| 103 | + const tracker = new CallTracker() |
| 104 | + const fakeLog = (value: unknown, ctx: Context) => {} |
| 105 | + const logSpy = tracker.calls(fakeLog, 1) |
| 106 | + assertValue('present', deprecated(any(), logSpy)) |
| 107 | + tracker.verify() |
| 108 | + }) |
| 109 | + }) |
86 | 110 | })
|
87 | 111 |
|
88 | 112 | /**
|
89 | 113 | * A helper for testing type signatures.
|
90 | 114 | */
|
91 | 115 |
|
92 | 116 | export function test<T>(fn: (x: unknown) => T) {}
|
| 117 | + |
| 118 | +/** |
| 119 | + * This emulates `tracker.calls(0)`. |
| 120 | + * |
| 121 | + * `CallTracker.calls` doesn't support passing `0`, therefore we expect it |
| 122 | + * to be called once which is our call in this test. This proves that |
| 123 | + * the following action didn't call it. |
| 124 | + */ |
| 125 | +function buildSpyWithZeroCalls(tracker: CallTracker) { |
| 126 | + const logSpy = tracker.calls(1) |
| 127 | + logSpy() |
| 128 | + return logSpy |
| 129 | +} |
0 commit comments