Assign instance of a class to a constant and give it default fixture in constructor. Fixture generator will return this fixture as default when using method getEntity().
const defaultFixture = {
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
const entityGenerator = new JsonApiEntityGenerator<EntityType>(defaultFixture);
Use method setEntities()
to set default entities. Use number of entities as first parameter and array of entity changes as second parameter. You don't have to use whole entity, just properties you want to change from default.
For example this:
entityGenerator.setEntities(3, [
{},
{
"attributes": {
"name": "second name"
"shortcut": "second shortcut"
}
}
]);
...will set entities as:
[
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "2"
"type": "example-entities",
"attributes": {
"name": "second name"
"shortcut": "second shortcut"
}
},
{
"id": "3"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
]
Use method getEntity()
to get fixture of default entity
entityGenerator.getEntity();
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": {
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
"included": [],
"links": {},
"meta": {
"total": 1,
"offset": 0,
"limit": 50
}
}
You can get modified entity when you fill entity changes into method parameter:
entityGenerator.getEntity({
"attributes": {
"name": "modified name"
}
});
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": {
"id": "1"
"type": "example-entities",
"attributes": {
"name": "modified name"
"shortcut": "exmp"
}
},
"included": [],
"links": {},
"meta": {
"total": 1,
"offset": 0,
"limit": 50
}
}
entityGenerator.getEntity({
"id": "modified-id"
});
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": {
"id": "modified-id"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
"included": [],
"links": {},
"meta": {
"total": 1,
"offset": 0,
"limit": 50
}
}
Calling method getEntities()
without any parameter will get you default enitites you set previously.
entityGenerator.getEntities();
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": [
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "2"
"type": "example-entities",
"attributes": {
"name": "second name"
"shortcut": "second shortcut"
}
},
{
"id": "3"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
],
"included": [],
"links": {},
"meta": {
"total": 3,
"offset": 0,
"limit": 50
}
}
Using first parameter numOfEntities you can get less or more entities than you set.
entityGenerator.getEntities(1);
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": [
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
],
"included": [],
"links": {},
"meta": {
"total": 1,
"offset": 0,
"limit": 50
}
}
If you ask for more entities than you set all additional will be defaultEntity with according "id":
entityGenerator.getEntities(5);
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": [
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "2"
"type": "example-entities",
"attributes": {
"name": "second name"
"shortcut": "second shortcut"
}
},
{
"id": "3"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "4"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "5"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
],
"included": [],
"links": {},
"meta": {
"total": 5,
"offset": 0,
"limit": 50
}
}
Using second parameter, you can set array of changes for entities you want return:
entityGenerator.getEntities(3, [
{
"attributes": {
"name": "modified name 1"
}
},
{},
{
"id": "modified-id"
"attributes": {
"name": "modified name 3"
}
}
]);
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": [
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "modified name 1"
"shortcut": "exmp"
}
},
{
"id": "2"
"type": "example-entities",
"attributes": {
"name": "second name"
"shortcut": "second shortcut"
}
},
{
"id": "modified-id"
"type": "example-entities",
"attributes": {
"name": "modified name 3"
"shortcut": "exmp"
}
}
],
"included": [],
"links": {},
"meta": {
"total": 3,
"offset": 0,
"limit": 50
}
}
If you want change for example just first entity, you don't have to specify other changes in the parameter. You will get rest of the entities those you set with method setEntities()
. For example if you want to modify just first entity:
entityGenerator.getEntities(3, [{
"attributes": {
"name": "modified name"
}
}]);
If you want to omit some entity/entities (scenario when you delete one of those entities), you can set it as null in entity changes and it will be omited.
entityGenerator.getEntities(3, [
{},
null
]);
...returns:
{
"jsonapi": {
"version": '1.0'
},
"data": [
{
"id": "1"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
},
{
"id": "3"
"type": "example-entities",
"attributes": {
"name": "example name",
"shortcut": "exmp"
}
}
],
"included": [],
"links": {},
"meta": {
"total": 2,
"offset": 0,
"limit": 50
}
}