/
deprecate.test.ts
122 lines (114 loc) · 3.64 KB
/
deprecate.test.ts
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
import { deprecate, shouldBeRemoved, deprecateType } from "../deprecation"
import { GraphQLObjectType, GraphQLString } from "graphql"
describe("deprecation", () => {
describe("deprecate", () => {
it("generates a deprecation reason that encodes the version from whence the field will be removed", () => {
expect(
deprecate({ inVersion: 2, preferUsageOf: "foo" })
).toMatchInlineSnapshot(
`"Prefer to use \`foo\`. [Will be removed in v2]"`
)
})
it("appends the encoded version from whence the field will be removed", () => {
expect(
deprecate({ inVersion: 2, reason: "Just don't do it." })
).toMatchInlineSnapshot(`"Just don't do it. [Will be removed in v2]"`)
})
})
describe("deprecateType", () => {
it("marks all fields of a type as deprecated", () => {
const type = deprecateType(
{ inVersion: 2, preferUsageOf: "AnotherType" },
new GraphQLObjectType({
name: "DeprecatedType",
fields: {
aField: {
type: GraphQLString,
},
anotherField: {
type: GraphQLString,
},
},
})
)
expect(type.getFields().aField.deprecationReason).toMatchInlineSnapshot(
`"The \`DeprecatedType\` type has been deprecated. Prefer to use the \`AnotherType\` type instead. [Will be removed in v2]"`
)
expect(
type.getFields().anotherField.deprecationReason
).toMatchInlineSnapshot(
`"The \`DeprecatedType\` type has been deprecated. Prefer to use the \`AnotherType\` type instead. [Will be removed in v2]"`
)
})
it("lazily evaluates a fields thunk", () => {
const thunk = jest.fn(() => ({
aField: {
type: GraphQLString,
},
}))
const type = deprecateType(
{ inVersion: 2, preferUsageOf: "AnotherType" },
new GraphQLObjectType({
name: "DeprecatedType",
fields: thunk,
})
)
expect(thunk).not.toHaveBeenCalled()
expect(type.getFields().aField.deprecationReason).not.toBeUndefined()
expect(thunk).toHaveBeenCalled()
})
it("does not mutate existing field config objects", () => {
const aField = {
type: GraphQLString,
}
deprecateType(
{ inVersion: 2, preferUsageOf: "AnotherType" },
new GraphQLObjectType({
name: "DeprecatedType",
fields: { aField },
})
)
expect(aField["deprecationReason"]).toBeUndefined()
})
})
describe("shouldBeRemoved", () => {
it("should not be removed if there is no deprecation reason", () => {
expect(
shouldBeRemoved({
deprecationReason: null,
inVersion: 2,
typeName: "Foo",
fieldName: "bar",
})
).toEqual(false)
expect(
shouldBeRemoved({
deprecationReason: undefined,
inVersion: 2,
typeName: "Foo",
fieldName: "bar",
})
).toEqual(false)
})
it("should not be removed if the deprecation is for a newer version", () => {
expect(
shouldBeRemoved({
deprecationReason: deprecate({ preferUsageOf: "foo", inVersion: 2 }),
inVersion: 3,
typeName: "Foo",
fieldName: "bar",
})
).toEqual(false)
})
it("should be removed if the deprecation is for the given version", () => {
expect(
shouldBeRemoved({
deprecationReason: deprecate({ preferUsageOf: "foo", inVersion: 2 }),
inVersion: 2,
typeName: "Foo",
fieldName: "bar",
})
).toEqual(true)
})
})
})