| 1 |
import { Server, Model, Factory, hasMany, belongsTo } from "miragejs"; |
| 2 |
import { inflections, pluralize, singularize } from "inflected"; |
| 3 |
|
| 4 |
// eslint-disable-next-line no-console |
| 5 |
let originalWarn = console.warn; |
| 6 |
|
| 7 |
function expectNoWarning() { |
| 8 |
// eslint-disable-next-line no-console |
| 9 |
console.warn = () => { |
| 10 |
expect(true).toBeFalsy(); |
| 11 |
}; |
| 12 |
} |
| 13 |
|
| 14 |
describe("External | Shared | Factories | create and createList", function () { |
| 15 |
let server, Contact, AmazingContact, Post, Author, Data; |
| 16 |
|
| 17 |
beforeEach(function () { |
| 18 |
inflections("en", (inflect) => { |
| 19 |
inflect.uncountable("data"); |
| 20 |
}); |
| 21 |
|
| 22 |
Contact = Model.extend(); |
| 23 |
AmazingContact = Model.extend(); |
| 24 |
Post = Model.extend({ |
| 25 |
author: belongsTo(), |
| 26 |
}); |
| 27 |
Author = Model.extend({ |
| 28 |
posts: hasMany(), |
| 29 |
}); |
| 30 |
Data = Model.extend(); |
| 31 |
|
| 32 |
server = new Server({ |
| 33 |
environment: "test", |
| 34 |
inflector: { pluralize, singularize }, |
| 35 |
models: { |
| 36 |
contact: Contact, |
| 37 |
amazingContact: AmazingContact, |
| 38 |
post: Post, |
| 39 |
author: Author, |
| 40 |
data: Data, |
| 41 |
}, |
| 42 |
factories: { |
| 43 |
contact: Factory.extend({ |
| 44 |
name: "Yehuda", |
| 45 |
}), |
| 46 |
amazingContact: Factory, |
| 47 |
}, |
| 48 |
}); |
| 49 |
}); |
| 50 |
|
| 51 |
afterEach(function () { |
| 52 |
server.shutdown(); |
| 53 |
|
| 54 |
// eslint-disable-next-line no-console |
| 55 |
console.warn = originalWarn; |
| 56 |
}); |
| 57 |
|
| 58 |
test("create throws when passing in an undefined model", () => { |
| 59 |
expectNoWarning(); |
| 60 |
|
| 61 |
expect(() => { |
| 62 |
server.create("foo"); |
| 63 |
}).toThrow( |
| 64 |
`You called server.create('foo') but no model or factory was found.` |
| 65 |
); |
| 66 |
}); |
| 67 |
|
| 68 |
// This used to be deprecated behavior, but now it errors. So we test it separately from the nonsense test above. |
| 69 |
test("create throws when passing in a pluralized version of a model", () => { |
| 70 |
expect.assertions(1); |
| 71 |
|
| 72 |
expect(() => { |
| 73 |
server.create("contacts"); |
| 74 |
}).toThrow( |
| 75 |
`You called server.create('contacts') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name` |
| 76 |
); |
| 77 |
}); |
| 78 |
|
| 79 |
test("create returns a Model if one is defined", () => { |
| 80 |
expectNoWarning(); |
| 81 |
|
| 82 |
let contact = server.create("contact"); |
| 83 |
|
| 84 |
expect(contact instanceof Contact).toBeTruthy(); |
| 85 |
expect(contact.name).toEqual("Yehuda"); |
| 86 |
}); |
| 87 |
|
| 88 |
test("create returns a Model instance if the Model name is uncountable", () => { |
| 89 |
expectNoWarning(); |
| 90 |
|
| 91 |
let data = server.create("data"); |
| 92 |
|
| 93 |
expect(data instanceof Data).toBeTruthy(); |
| 94 |
}); |
| 95 |
|
| 96 |
test("createList throws when passing in an undefined model", () => { |
| 97 |
expectNoWarning(); |
| 98 |
|
| 99 |
expect(() => { |
| 100 |
server.createList("foo", 1); |
| 101 |
}).toThrow( |
| 102 |
`You called server.createList('foo') but no model or factory was found.` |
| 103 |
); |
| 104 |
}); |
| 105 |
|
| 106 |
// This used to be deprecated behavior, but now it errors. So we test it separately from the nonsense test above. |
| 107 |
test("createList throws when passing in a pluralized version of a model", () => { |
| 108 |
expect.assertions(1); |
| 109 |
|
| 110 |
expect(() => { |
| 111 |
server.createList("contacts", 1); |
| 112 |
}).toThrow( |
| 113 |
`You called server.createList('contacts') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name.` |
| 114 |
); |
| 115 |
}); |
| 116 |
|
| 117 |
test("createList returns Models if one is defined", () => { |
| 118 |
expectNoWarning(); |
| 119 |
|
| 120 |
let contacts = server.createList("contact", 1); |
| 121 |
|
| 122 |
expect(contacts[0] instanceof Contact).toBeTruthy(); |
| 123 |
expect(contacts[0].name).toEqual("Yehuda"); |
| 124 |
}); |
| 125 |
|
| 126 |
test("createList returns Models if the model name is uncountable", () => { |
| 127 |
expectNoWarning(); |
| 128 |
|
| 129 |
let data = server.createList("data", 1); |
| 130 |
|
| 131 |
expect(data[0] instanceof Data).toBeTruthy(); |
| 132 |
}); |
| 133 |
|
| 134 |
test("create returns a Model if one is defined, when using a compound name", () => { |
| 135 |
expectNoWarning(); |
| 136 |
|
| 137 |
let contact = server.create("amazing-contact"); |
| 138 |
|
| 139 |
expect(contact instanceof AmazingContact).toBeTruthy(); |
| 140 |
}); |
| 141 |
|
| 142 |
test("createList returns Models if one is defined, when using a compound name", () => { |
| 143 |
expectNoWarning(); |
| 144 |
|
| 145 |
let contacts = server.createList("amazing-contact", 1); |
| 146 |
|
| 147 |
expect(contacts[0] instanceof AmazingContact).toBeTruthy(); |
| 148 |
}); |
| 149 |
|
| 150 |
test("create falls back to a model if no factory is defined", () => { |
| 151 |
expectNoWarning(); |
| 152 |
|
| 153 |
let post = server.create("post"); |
| 154 |
|
| 155 |
expect(post instanceof Post).toBeTruthy(); |
| 156 |
expect(post.id).toEqual("1"); |
| 157 |
}); |
| 158 |
|
| 159 |
test("createList falls back to a model if no factory is defined", () => { |
| 160 |
expectNoWarning(); |
| 161 |
|
| 162 |
let posts = server.createList("post", 2); |
| 163 |
|
| 164 |
expect(posts[0] instanceof Post).toBeTruthy(); |
| 165 |
expect(posts).toHaveLength(2); |
| 166 |
expect(posts[0].id).toEqual("1"); |
| 167 |
}); |
| 168 |
|
| 169 |
test("create sets up the db correctly when passing in fks", () => { |
| 170 |
expectNoWarning(); |
| 171 |
|
| 172 |
let author = server.create("author"); |
| 173 |
let post = server.create("post", { |
| 174 |
authorId: author.id, |
| 175 |
}); |
| 176 |
author.reload(); |
| 177 |
|
| 178 |
expect(author.posts.models).toHaveLength(1); |
| 179 |
expect(post.author.attrs).toEqual(author.attrs); |
| 180 |
expect(server.db.posts[0].authorId).toEqual(author.id); |
| 181 |
}); |
| 182 |
|
| 183 |
test("create sets up the db correctly when passing in models", () => { |
| 184 |
expectNoWarning(); |
| 185 |
|
| 186 |
let author = server.create("author"); |
| 187 |
let post = server.create("post", { |
| 188 |
author, |
| 189 |
}); |
| 190 |
|
| 191 |
expect(author.posts.models).toHaveLength(1); |
| 192 |
expect(post.author.attrs).toEqual(author.attrs); |
| 193 |
expect(server.db.posts[0].authorId).toEqual(author.id); |
| 194 |
}); |
| 195 |
}); |