| 1 |
import { |
| 2 |
_ormSchema as Schema, |
| 3 |
_Db as Db, |
| 4 |
Model, |
| 5 |
belongsTo, |
| 6 |
ActiveModelSerializer, |
| 7 |
} from "@lib"; |
| 8 |
|
| 9 |
describe("Unit | Serializers | ActiveModelSerializer", function () { |
| 10 |
let serializer = null; |
| 11 |
let schema = null; |
| 12 |
|
| 13 |
beforeEach(function () { |
| 14 |
schema = new Schema(new Db(), { |
| 15 |
contact: Model.extend({ |
| 16 |
address: belongsTo(), |
| 17 |
}), |
| 18 |
address: Model.extend({ |
| 19 |
contact: belongsTo(), |
| 20 |
}), |
| 21 |
}); |
| 22 |
serializer = new ActiveModelSerializer({ |
| 23 |
schema, |
| 24 |
}); |
| 25 |
}); |
| 26 |
|
| 27 |
test("normalize works", () => { |
| 28 |
let payload = { |
| 29 |
contact: { |
| 30 |
id: 1, |
| 31 |
name: "Link", |
| 32 |
}, |
| 33 |
}; |
| 34 |
let jsonApiDoc = serializer.normalize(payload); |
| 35 |
|
| 36 |
expect(jsonApiDoc).toEqual({ |
| 37 |
data: { |
| 38 |
type: "contacts", |
| 39 |
id: 1, |
| 40 |
attributes: { |
| 41 |
name: "Link", |
| 42 |
}, |
| 43 |
}, |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
test("it hyphenates snake_cased words", () => { |
| 48 |
let payload = { |
| 49 |
contact: { |
| 50 |
id: 1, |
| 51 |
first_name: "Link", |
| 52 |
}, |
| 53 |
}; |
| 54 |
let jsonApiDoc = serializer.normalize(payload); |
| 55 |
|
| 56 |
expect(jsonApiDoc).toEqual({ |
| 57 |
data: { |
| 58 |
type: "contacts", |
| 59 |
id: 1, |
| 60 |
attributes: { |
| 61 |
"first-name": "Link", |
| 62 |
}, |
| 63 |
}, |
| 64 |
}); |
| 65 |
}); |
| 66 |
|
| 67 |
test("it works without an id", () => { |
| 68 |
let payload = { |
| 69 |
contact: { |
| 70 |
first_name: "Link", |
| 71 |
last_name: "zor", |
| 72 |
}, |
| 73 |
}; |
| 74 |
let jsonApiDoc = serializer.normalize(payload); |
| 75 |
|
| 76 |
expect(jsonApiDoc).toEqual({ |
| 77 |
data: { |
| 78 |
type: "contacts", |
| 79 |
attributes: { |
| 80 |
"first-name": "Link", |
| 81 |
"last-name": "zor", |
| 82 |
}, |
| 83 |
}, |
| 84 |
}); |
| 85 |
}); |
| 86 |
|
| 87 |
test("it returns coalesce Ids if present", () => { |
| 88 |
let request = { url: "/authors", queryParams: { ids: ["1", "3"] } }; |
| 89 |
expect(serializer.getCoalescedIds(request)).toEqual(["1", "3"]); |
| 90 |
}); |
| 91 |
|
| 92 |
test("it returns undefined coalesce Ids if not present", () => { |
| 93 |
let request = { url: "/authors", queryParams: {} }; |
| 94 |
expect(serializer.getCoalescedIds(request)).toBeUndefined(); |
| 95 |
}); |
| 96 |
|
| 97 |
test("normalizeIds defaults to true", () => { |
| 98 |
let serializer = new ActiveModelSerializer(); |
| 99 |
|
| 100 |
expect(serializer.normalizeIds).toEqual(true); |
| 101 |
}); |
| 102 |
|
| 103 |
test("normalize works with normalizeIds set to true", () => { |
| 104 |
serializer.normalizeIds = true; |
| 105 |
let payload = { |
| 106 |
contact: { |
| 107 |
id: 1, |
| 108 |
name: "Link", |
| 109 |
address: 1, |
| 110 |
}, |
| 111 |
}; |
| 112 |
let jsonApiDoc = serializer.normalize(payload); |
| 113 |
|
| 114 |
expect(jsonApiDoc).toEqual({ |
| 115 |
data: { |
| 116 |
type: "contacts", |
| 117 |
id: 1, |
| 118 |
attributes: { |
| 119 |
name: "Link", |
| 120 |
}, |
| 121 |
relationships: { |
| 122 |
address: { |
| 123 |
data: { |
| 124 |
type: "address", |
| 125 |
id: 1, |
| 126 |
}, |
| 127 |
}, |
| 128 |
}, |
| 129 |
}, |
| 130 |
}); |
| 131 |
}); |
| 132 |
|
| 133 |
test('serializeIds defaults to "always"', () => { |
| 134 |
let defaultState = new ActiveModelSerializer(); |
| 135 |
expect(defaultState.serializeIds).toEqual("always"); |
| 136 |
}); |
| 137 |
}); |