-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add different way to define relations on models
- Loading branch information
1 parent
f1462a1
commit 74bec09
Showing
6 changed files
with
217 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export class Relation { | ||
__toModel = null; | ||
|
||
constructor(toModel) { | ||
this.__toModel = toModel | ||
} | ||
|
||
get model() { | ||
return this.__toModel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Kind, Person, PersonStore} from "./fixtures/Animal"; | ||
import {observable} from "mobx"; | ||
import Model from "../Model"; | ||
import {BinderApi} from "../index"; | ||
|
||
class Animal extends Model { | ||
urlRoot = '/api/animal/'; | ||
api = new BinderApi(); | ||
static backendResourceName = 'animal'; | ||
@observable id = null; | ||
@observable name = ''; | ||
|
||
@observable kind = this.relation(Kind); | ||
|
||
@observable owner = this.relation(Person); | ||
|
||
@observable pastOwner = this.relation(PersonStore) | ||
|
||
@observable father = this.relation(Animal) | ||
} | ||
|
||
describe('Model with new way of defining relations', () => { | ||
test('Initialize model with valid data', () => { | ||
const animal = new Animal({}) | ||
}); | ||
|
||
test('Relation should not be initialized by default', () => { | ||
const animal = new Animal(); | ||
|
||
expect(animal.kind).toBeUndefined(); | ||
}); | ||
|
||
test('Initialize one-level relation', () => { | ||
const animal = new Animal(null, { | ||
relations: ['kind'], | ||
}); | ||
|
||
expect(animal.kind).toBeInstanceOf(Kind); | ||
}); | ||
test('Initialize multiple relations', () => { | ||
const animal = new Animal(null, { | ||
relations: ['kind', 'owner'], | ||
}); | ||
|
||
expect(animal.kind).toBeInstanceOf(Kind); | ||
expect(animal.owner).toBeInstanceOf(Person); | ||
}); | ||
|
||
test('Non existent relation should throw an error', () => { | ||
expect(() => { | ||
return new Animal(null, { | ||
relations: ['ponyfoo'], | ||
}); | ||
}).toThrow('Specified relation "ponyfoo" does not exist on model.'); | ||
}); | ||
|
||
test('Can have a relation to itself', () => { | ||
const animal = new Animal(null, { | ||
relations: ['father'], | ||
}); | ||
|
||
expect(animal.father).toBeInstanceOf(Animal); | ||
}) | ||
|
||
|
||
}) | ||
|
||
|
||
|