Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TransactionSpec.js « objects « api « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 286401ad18ee1b299c09ca386103ce94c3bec99d (plain)
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
import Transaction from "./Transaction";
import utils from 'objectUtils';

let openmct = {};
let objectAPI;
let transaction;

describe("Transaction Class", () => {
    beforeEach(() => {
        objectAPI = {
            makeKeyString: (identifier) => utils.makeKeyString(identifier),
            save: () => Promise.resolve(true),
            mutate: (object, prop, value) => {
                object[prop] = value;

                return object;
            },
            refresh: (object) => Promise.resolve(object),
            areIdsEqual: (...identifiers) => {
                return identifiers.map(utils.parseKeyString)
                    .every(identifier => {
                        return identifier === identifiers[0]
                            || (identifier.namespace === identifiers[0].namespace
                                && identifier.key === identifiers[0].key);
                    });
            }
        };

        transaction = new Transaction(objectAPI);

        openmct.editor = {
            isEditing: () => true
        };
    });

    it('has no dirty objects', () => {
        expect(transaction.dirtyObjects.size).toEqual(0);
    });

    it('add(), adds object to dirtyObjects', () => {
        const mockDomainObjects = createMockDomainObjects();
        transaction.add(mockDomainObjects[0]);
        expect(transaction.dirtyObjects.size).toEqual(1);
    });

    it('cancel(), clears all dirtyObjects', (done) => {
        const mockDomainObjects = createMockDomainObjects(3);
        mockDomainObjects.forEach(transaction.add.bind(transaction));

        expect(transaction.dirtyObjects.size).toEqual(3);

        transaction.cancel()
            .then(success => {
                expect(transaction.dirtyObjects.size).toEqual(0);
            }).finally(done);
    });

    it('commit(), saves all dirtyObjects', (done) => {
        const mockDomainObjects = createMockDomainObjects(3);
        mockDomainObjects.forEach(transaction.add.bind(transaction));

        expect(transaction.dirtyObjects.size).toEqual(3);
        spyOn(objectAPI, 'save').and.callThrough();

        transaction.commit()
            .then(success => {
                expect(transaction.dirtyObjects.size).toEqual(0);
                expect(objectAPI.save.calls.count()).toEqual(3);
            }).finally(done);
    });

    it('getDirtyObject(), returns correct dirtyObject', () => {
        const mockDomainObjects = createMockDomainObjects();
        transaction.add(mockDomainObjects[0]);

        expect(transaction.dirtyObjects.size).toEqual(1);
        const dirtyObject = transaction.getDirtyObject(mockDomainObjects[0].identifier);

        expect(dirtyObject).toEqual(mockDomainObjects[0]);
    });

    it('getDirtyObject(), returns empty dirtyObject for no active transaction', () => {
        const mockDomainObjects = createMockDomainObjects();

        expect(transaction.dirtyObjects.size).toEqual(0);
        const dirtyObject = transaction.getDirtyObject(mockDomainObjects[0].identifier);

        expect(dirtyObject).toEqual(undefined);
    });
});

function createMockDomainObjects(size = 1) {
    const objects = [];

    while (size > 0) {
        const mockDomainObject = {
            identifier: {
                namespace: 'test-namespace',
                key: `test-key-${size}`
            },
            name: `test object ${size}`,
            type: 'test-type'
        };

        objects.push(mockDomainObject);

        size--;
    }

    return objects;
}