All files / mobx-decorated-models/specs test-models.js

95.83% Statements 23/24
90% Branches 9/10
93.75% Functions 15/16
95.24% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                53x 53x               1x     7x     1x       3x   3x 7x 2x             6x     10x                                   16x                 2x                                     7x                   41x 1x             3x   1x                                             30x           2x                        
import { observable, computed } from 'mobx';
import {
    identifiedBy, field, session, belongsTo, hasMany, identifier, registerCustomType,
} from '../index';
 
class RectangularCuboid {
 
    constructor(attrs) {
        Object.assign(this, attrs);
        this.isCuboid = true;
    }
 
}
 
export class Radio {
 
    constructor(frequency) {
        this.frequency = frequency;
    }
    static serialize(radio) {
        return radio ? radio.frequency : null;
    }
    static deserialize(frequency) {
        return new Radio(frequency);
    }
 
}
registerCustomType('radio', Radio);
 
const shipCargoSerializer = [
    a => (a ? a - 1 : a),
    b => (b ? b + 3 : b),
];
 
@identifiedBy('registration')
export class Registration {
 
    constructor(id) {
        this.id = id;
    }
    static serialize(reg) {
        return reg ? reg.id : '';
    }
 
}
 
@identifiedBy('boat')
export class Ship {
 
    @identifier name;
    @field({ model: 'registration' }) registration;
    @field({ type: 'radio' }) radio;
    @field({ serializer: shipCargoSerializer }) cargoCount;
    @field({ type: 'date' }) embarks;
    @belongsTo({ inverseOf: 'vessel' }) box;
    @hasMany({ model: Registration }) homePorts;
 
    constructor(attrs) {
 
        if (this.constructor.buildSpy) { this.constructor.buildSpy(attrs); }
    }
 
}
 
@identifiedBy('dimension')
export class Dimension {
 
    constructor(attrs) {
        Object.assign(this, attrs);
    }
 
}
 
@identifiedBy('box')
export class Box extends RectangularCuboid {
 
    @identifier id;
 
    @field width  = 1;
    @field height = 1;
    @field depth  = 1;
    @field({ type: 'object' }) metadata;
 
    @session color;
    @session vessel_association_name;
 
    @computed get volume() {
        return this.width * this.height * this.depth;
    }
    @belongsTo vessel;
    @observable vessel_association_name;
    @belongsTo({ model: 'boat' }) watercraft;
    @belongsTo container;
 
    @hasMany({
        model: Dimension,
        extend: (array) => {
            Object.defineProperty(array, 'volume', {
                get() { return this.length * 2; },
            });
        },
    }) sides;
 
}
 
const BoxExtensions = {
    identifiers() {
        return this.map(b => b.id);
    },
};
 
@identifiedBy('container')
export class Container extends RectangularCuboid {
 
    @identifier id;
 
    @field name;
    @field location;
    @field({ type: 'array' }) tags = [];
 
    @session color;
 
    @computed get description() {
        return `${this.name} ${this.location}`;
    }
 
    @hasMany({
        model: 'box',
        inverseOf: 'container',
        defaults() {
            return { color: this.color };
        },
        extend: BoxExtensions,
    }) boxes;
 
    @computed get areaInUse() {
        return this.boxes.reduce((acc, box) => (acc + box.volume), 0);
    }
 
}
 
export class NonIdentified {
 
    @identifier id;
    @field name;
    @belongsTo({ model: Box }) box;
 
}