| 1 |
import { |
| 2 |
Model, |
| 3 |
hasMany, |
| 4 |
belongsTo, |
| 5 |
Server, |
| 6 |
IdentityManager as DefaultIdentityManager, |
| 7 |
} from "miragejs"; |
| 8 |
|
| 9 |
const CustomIdentityManager = class { |
| 10 |
constructor() { |
| 11 |
this.wasCalled = false; |
| 12 |
} |
| 13 |
fetch() { |
| 14 |
if (this.wasCalled) { |
| 15 |
throw new Error( |
| 16 |
"IdentityManager used for test only supports one call to fetch" |
| 17 |
); |
| 18 |
} |
| 19 |
this.wasCalled = true; |
| 20 |
return "custom-id"; |
| 21 |
} |
| 22 |
set(id) { |
| 23 |
throw new Error("Not implemented for test."); |
| 24 |
} |
| 25 |
reset() { |
| 26 |
throw new Error("Not implemented for test."); |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
describe("External | Shared | Identity manager", function () { |
| 31 |
let server; |
| 32 |
|
| 33 |
afterEach(() => { |
| 34 |
server.shutdown(); |
| 35 |
}); |
| 36 |
|
| 37 |
test("it uses identity managers defined by config", () => { |
| 38 |
server = new Server({ |
| 39 |
environment: "test", |
| 40 |
identityManagers: { |
| 41 |
post: DefaultIdentityManager, |
| 42 |
author: CustomIdentityManager, |
| 43 |
}, |
| 44 |
models: { |
| 45 |
author: Model.extend({ |
| 46 |
posts: hasMany(), |
| 47 |
}), |
| 48 |
comment: Model.extend({ |
| 49 |
post: belongsTo(), |
| 50 |
}), |
| 51 |
post: Model.extend({ |
| 52 |
author: belongsTo(), |
| 53 |
}), |
| 54 |
}, |
| 55 |
}); |
| 56 |
|
| 57 |
let author = server.create("author"); |
| 58 |
let comment = server.create("comment"); |
| 59 |
let post = server.create("post"); |
| 60 |
|
| 61 |
expect(author.id).toEqual("custom-id"); |
| 62 |
expect(post.id).toEqual("1"); |
| 63 |
expect(comment.id).toEqual("1"); |
| 64 |
}); |
| 65 |
|
| 66 |
test("identity managers can use record data in their fetch method", () => { |
| 67 |
let IdentityManagerForTest = class { |
| 68 |
fetch(data) { |
| 69 |
return `${data.ssn}`; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
server = new Server({ |
| 74 |
environment: "test", |
| 75 |
identityManagers: { |
| 76 |
application: IdentityManagerForTest, |
| 77 |
}, |
| 78 |
models: { |
| 79 |
user: Model.extend(), |
| 80 |
}, |
| 81 |
}); |
| 82 |
|
| 83 |
let ryan = server.create("user", { name: "Ryan", ssn: 123456789 }); |
| 84 |
|
| 85 |
expect(ryan.id).toEqual("123456789"); |
| 86 |
}); |
| 87 |
}); |