1 | var assert = require('assert'),
|
2 | describe = require('Jody').describe,
|
3 | Model = require('../lib/index');
|
4 |
|
5 |
|
6 | describe("Model can have custom functions", function (spec) {
|
7 |
|
8 | spec.beforeAll(function (done) {
|
9 | var Vehicle = Model.define('Vehicle', {
|
10 | name: String,
|
11 | gear: {type: Number, default: 1}
|
12 | });
|
13 |
|
14 | Model.define('Airplane', {name: String});
|
15 |
|
16 | Vehicle.addMethod('sound', function () {
|
17 | return "Rooooooooom";
|
18 | });
|
19 |
|
20 | Vehicle.addMethod('change_gear', function (gear) {
|
21 | var old_gear = this.gear;
|
22 | this.gear = gear;
|
23 |
|
24 | return "changing from " + old_gear + " to " + gear;
|
25 | });
|
26 |
|
27 | Model.load();
|
28 | done();
|
29 | });
|
30 |
|
31 | spec.it("Should have custom defined method", function () {
|
32 | var Vehicle = Model('Vehicle');
|
33 |
|
34 | var ford = Vehicle.create({name: "Ford Fiesta"});
|
35 |
|
36 | assert.notEqual(ford.sound, null);
|
37 | });
|
38 |
|
39 | spec.it("Should be able to be called", function () {
|
40 | var Vehicle = Model('Vehicle');
|
41 |
|
42 | var bmw = Vehicle.create({name: "BMW"});
|
43 |
|
44 | bmw.sound().should().beEqual("Rooooooooom");
|
45 |
|
46 | });
|
47 |
|
48 | spec.it("Should be able to pass parameters to it", function () {
|
49 | var Vehicle = Model('Vehicle');
|
50 |
|
51 | var ford = Vehicle.create({name: "Ford Fiesta"});
|
52 | ford.change_gear(3).should().beEqual("changing from 1 to 3");
|
53 |
|
54 | });
|
55 |
|
56 | spec.it("Should not be accessible on other models", function () {
|
57 | var Airplane = Model('Airplane');
|
58 | var cessna = Airplane.create({name: "Cessna"});
|
59 |
|
60 | assert.equal(cessna.sound,null)
|
61 |
|
62 | });
|
63 |
|
64 |
|
65 | }); |
\ | No newline at end of file |