Apologies in advance: I am new to both Ember and Mirage, and I am tasked with creating acceptance tests for an existing Ember application. My challenge is in using Mirage (0.1.13) and fixtures (not factories) to mock existing one-to-many or many-to-many relationships in our data. I couldn't find any complete examples that model non-trivial relationships.
Consider the following simple many-to-many relationship: A user can have multiple roles.
The respective Ember models are:
// models/user.js
import Ember from "ember";
import DS from 'ember-data';
export default DS.Model.extend({
email: DS.attr('string'),
//unidirectional M-M
roles: DS.hasMany('user-role', {inverse: null})
});
// models/user-role.js
import DS from 'ember-data';
export default DS.Model.extend({
description: DS.attr('string')
});
Note that user-role
is hyphenated.
My attempt at creating the fixtures:
// mirage/fixtures/users.js
export default [
{id: 1, email: 'user@email.com', roles: {userRole_ids: [1]}},
{id: 2, email: 'supervisor@email.com', roles: {userRole_ids: [1, 2]}}
];
// mirage/fixtures/userRoles.js
export default [
{id: 1, description: 'regular user'},
{id: 2, description: 'supervisor'}
];
//mirage/config.js
export default function() {
//...
//single user data
this.get('/users/:id', function(db, request) {
let id = request.params.id;
return db.users.find(id);
});
//...
}
When I place a breakpoint in finders.js
on
var payload = normalizeResponseHelper(serializer, store, typeClass, adapterPayload, id, 'findRecord');
and inspect the value of adapterPayload
I see, e.g.,
email: "supervisor@email.com"
id: 2
roles: Array[2]
0: Object:
userRole_id: 1
1: Object:
userRole_id: 2
instead of the expected
email: "supervisor@email.com"
id: 2
roles: Array[2]
0: Object
id: 1
description: "regular user"
1: Object
id: 2
description: "supervisor"
In other words, the user role ID foreign key is not resolved by Mirage into its corresponding record. I suspect it has something to do with the hyphenation in user-role not matching my naming of the userRoles.js
file.
The back-end functionality (that I am attempting to mock) is implemented using JAX-RS JPA and the Spring Framework, shown below:
// User.java
//...
@Entity
@Table(name = "User")
public class User extends BaseBizObj<User, SimplePrimaryKey<String>, UserRepository> {
//...
@ManyToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "Roles")
@NotNull
@JsonProperty
private Set<UserRole> roles;
//...
}
// UserRole.java
//...
@Entity
@Table(name = "UserRole")
public class User extends BaseBizObj<User, SimplePrimaryKey<String>, UserRoleRepository> {
//...
@Column(name = "Description")
@NotNull
@JsonProperty
private String description;
//...
}
Any help will be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire