Sha256: 293cb7b88da1f001d0cc8687b19b3e7ce7af345a12377d9d32af87cb7205d586

Contents?: true

Size: 1.9 KB

Versions: 9

Compression:

Stored size: 1.9 KB

Contents

import { autorun } from 'mobx';
import { Box } from '../test-models';

describe('Model Collection Test', () => {
    it('adds items specific for each model', () => {
        const collection = Box.Collection.create();
        expect(Box.identifiedBy).toEqual('test/box');
        collection.push({ width: 3, height: 3, depth: 3 });
        const box = collection[0];
        expect(box).toBeInstanceOf(Box);
        expect(box.volume).toBe(27);
    });

    it('has custom properties are observable', () => {
        const collection = Box.Collection.create();
        const spy = jest.fn();
        autorun(() => { spy(collection.lastServerMessage); });
        expect(spy).toHaveBeenCalledTimes(1);
        collection.lastServerMessage = 'test-2';
        expect(collection.lastServerMessage).toEqual('test-2');
        expect(spy).toHaveBeenCalledTimes(2);
        expect(spy).toHaveBeenLastCalledWith('test-2');
    });

    it('can fetch', () => {
        fetch.mockResponseOnce(JSON.stringify({ data: [{ width: 12, height: 12, depth: 10 }] }));
        Box.Collection.create().fetch().then((collection) => {
            expect(fetch).lastCalledWith(
                '/api/test/boxes.json',
                { headers: { 'Content-Type': 'application/json' } },
            );
            expect(collection.length).toEqual(1);
            expect(collection[0].volume).toBe(1440);
        });
    });

    it('can be initialized with models', () => {
        const collection = Box.Collection.create([{ width: 3, height: 3, depth: 3 }]);
        expect(collection[0].volume).toBe(27);
    });

    it('can set default attributes', () => {
        const collection = Box.Collection.create(
            [{ width: 3 }],
            { defaults: { label: 'paper box' } },
        );
        expect(collection[0].width).toBe(3); // doesn't overwrite
        collection.push({ });
        expect(collection[1].label).toEqual('paper box');
    });
});

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hippo-fw-0.9.9 spec/client/models/collection.spec.js
hippo-fw-0.9.8 spec/client/models/collection.spec.js
hippo-fw-0.9.7 spec/client/models/collection.spec.js
hippo-fw-0.9.6 spec/client/models/collection.spec.js
hippo-fw-0.9.5 spec/client/models/collection.spec.js
hippo-fw-0.9.4 spec/client/models/collection.spec.js
hippo-fw-0.9.3 spec/client/models/collection.spec.js
hippo-fw-0.9.2 spec/client/models/collection.spec.js
hippo-fw-0.9.1 spec/client/models/collection.spec.js