-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/nasa/openmct/issues/4369
- Loading branch information
1 parent
1da329c
commit 641bc2f
Showing
2 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
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: (object) => object, | ||
mutate: (object, prop, value) => { | ||
object[prop] = value; | ||
|
||
return object; | ||
}, | ||
refresh: (object) => Promise.resolve(object) | ||
}; | ||
|
||
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'); | ||
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; | ||
} |