summaryrefslogtreecommitdiffstats
path: root/apps/files_versions/tests/js/versionmodelSpec.js
blob: 0f1c06581d54ae29956f09a2eba0175b7d276e8f (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
/*
 * Copyright (c) 2015
 *
 * This file is licensed under the Affero General Public License version 3
 * or later.
 *
 * See the COPYING-README file.
 *
 */
describe('OCA.Versions.VersionModel', function() {
	var VersionModel = OCA.Versions.VersionModel;
	var model;

	beforeEach(function() {
		model = new VersionModel({
			id: 10000000,
			timestamp: 10000000,
			fullPath: '/subdir/some file.txt',
			name: 'some file.txt',
			size: 150
		});
	});

	it('returns the full path', function() {
		expect(model.getFullPath()).toEqual('/subdir/some file.txt');
	});
	it('returns the preview url', function() {
		expect(model.getPreviewUrl())
			.toEqual(OC.generateUrl('/apps/files_versions/preview') +
					'?file=%2Fsubdir%2Fsome%20file.txt&version=10000000'
			);
	});
	it('returns the download url', function() {
		expect(model.getDownloadUrl())
			.toEqual(OC.generateUrl('/apps/files_versions/download.php') +
					'?file=%2Fsubdir%2Fsome%20file.txt&revision=10000000'
			);
	});
	describe('reverting', function() {
		var revertEventStub;
		var successStub;
		var errorStub;

		beforeEach(function() {
			revertEventStub = sinon.stub();
			errorStub = sinon.stub();
			successStub = sinon.stub();

			model.on('revert', revertEventStub);
			model.on('error', errorStub);
		});
		it('tells the server to revert when calling the revert method', function() {
			model.revert({
				success: successStub
			});

			expect(fakeServer.requests.length).toEqual(1);
			expect(fakeServer.requests[0].url)
				.toEqual(
					OC.generateUrl('/apps/files_versions/ajax/rollbackVersion.php') +
					'?file=%2Fsubdir%2Fsome+file.txt&revision=10000000'
				);

			fakeServer.requests[0].respond(
				200,
				{ 'Content-Type': 'application/json' },
				JSON.stringify({
					status: 'success',
				})
			);

			expect(revertEventStub.calledOnce).toEqual(true);
			expect(successStub.calledOnce).toEqual(true);
			expect(errorStub.notCalled).toEqual(true);
		});
		it('triggers error event when server returns a failure', function() {
			model.revert({
				success: successStub
			});

			expect(fakeServer.requests.length).toEqual(1);
			fakeServer.requests[0].respond(
				200,
				{ 'Content-Type': 'application/json' },
				JSON.stringify({
					status: 'error',
				})
			);

			expect(revertEventStub.notCalled).toEqual(true);
			expect(successStub.notCalled).toEqual(true);
			expect(errorStub.calledOnce).toEqual(true);
		});
	});
});