/
sum-backend-filesystem.js
65 lines (52 loc) · 2.49 KB
/
sum-backend-filesystem.js
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
describe('backend filesystem', function() {
var backendFilesystem = inject('sum-backend-filesystem');
it('writeJsonFile: writes back a json file', function() {
var file = 'c:/tmp/test.json';
var content = { example: 'value1', example2: 'value2' };
fs = jasmine.createSpyObj('fs', ['writeFile']);
backendFilesystem.writeJsonFile(file, content);
expect(fs.writeFile).toHaveBeenCalledWith(file, JSON.stringify(content, null, 4), 'utf8', jasmine.any(Function));
});
it('writeJsonFile: handels error correctly with default error handler (which throws an error)', function() {
fs = jasmine.createSpyObj('fs', ['writeFile']);
fs.writeFile.and.throwError("bla");
expect(function() {
backendFilesystem.writeJsonFile("", "");
}).toThrowError("bla");
});
it('readJsonFile: reads json files and parses json correctly', function() {
var file = 'c:/tmp/test.json';
var content = "{ 'example': 'value1', 'example2': 'value2' }";
fs = jasmine.createSpyObj('fs', ['readFile']);
fs.readFile.and.callFake(function(file, format, callback) {
var success = function(data) {
expect(data).not.toBeUndefined();
expect(typeof data.example != 'undefined').toBe(true);
};
callback(undefined, content);
});
backendFilesystem.readJsonFile(file, function(data) {});
expect(fs.readFile).toHaveBeenCalledWith(file, 'utf8', jasmine.any(Function));
});
it('readFile: reads a file', function() {
var file = 'c:/tmp/test.json';
var testData = 'data123';
fs = jasmine.createSpyObj('fs', ['readFile']);
fs.readFile.and.callFake(function(file, callback) {
var success = function(data) {
expect(data).not.toBeUndefined();
expect(data).toBe(testData);
};
callback(undefined, testData);
});
backendFilesystem.readFile(file, function(data) {});
expect(fs.readFile).toHaveBeenCalledWith(file, jasmine.any(Function));
});
it('lock, unlock: starts get and release lockFile from lockfile lib', function() {
lockFile = jasmine.createSpyObj('lockFile', ['lock', 'unlock']);
backendFilesystem.lock();
backendFilesystem.unlock();
expect(lockFile.lock).toHaveBeenCalled();
expect(lockFile.unlock).toHaveBeenCalled();
});
});