Skip to content

Commit 59dce84

Browse files
committed
Add a test for formData
1 parent f213518 commit 59dce84

File tree

1 file changed

+93
-7
lines changed

1 file changed

+93
-7
lines changed

test/unit/spec/controllers/tryoperation/makeCall.js

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,22 @@ describe('Controller: TryOperation', function() {
1111
beforeEach(inject(function(_$controller_, $rootScope) {
1212
scope = $rootScope.$new();
1313
$controller = _$controller_;
14+
scope.operation = {};
15+
scope.specs = {};
16+
scope.getParameters = function mockGetParameters() {
17+
return [];
18+
};
19+
scope.$watch = function() {};
20+
$controller('TryOperation', {
21+
$scope: scope
22+
});
1423
}));
1524

1625
describe('$scope.makeCall', function() {
26+
afterEach(function () {
27+
$.ajax.restore();
28+
});
29+
1730
it('should call ajax when it\'s called', function() {
1831
var operation = {
1932
responses: {
@@ -36,13 +49,6 @@ describe('Controller: TryOperation', function() {
3649
};
3750
scope.pathName = '/';
3851
scope.operation = operation;
39-
scope.getParameters = function mockGetParameters() {
40-
return [];
41-
};
42-
scope.$watch = function() {};
43-
$controller('TryOperation', {
44-
$scope: scope
45-
});
4652

4753
var ajaxStub = sinon.stub($, 'ajax').returns({
4854
fail: function() {
@@ -54,5 +60,85 @@ describe('Controller: TryOperation', function() {
5460

5561
ajaxStub.should.have.been.called;
5662
});
63+
64+
describe('use server', function() {
65+
66+
it('should upload the file', function() {
67+
var parameters= [{
68+
name: 'upload',
69+
in: 'formData',
70+
required: true,
71+
schema: {
72+
type: 'file'
73+
}
74+
}];
75+
scope.specs = {
76+
host: 'localhost:3000',
77+
swagger: '2.0',
78+
info: {
79+
version: '0.0.0',
80+
title: 'Upload API'
81+
},
82+
security: [],
83+
consumes: [ 'multipart/form-data' ],
84+
paths: {
85+
'/': {
86+
post: {
87+
parameters: parameters,
88+
responses: {
89+
200: {
90+
description: "OK"
91+
}
92+
}
93+
}
94+
}
95+
}
96+
};
97+
scope.pathName = '/';
98+
scope.getParameters = function() {
99+
return [{
100+
name: 'upload',
101+
in: 'formData',
102+
required: true,
103+
type: 'file',
104+
schema: {
105+
type: 'file'
106+
}
107+
}];
108+
};
109+
110+
scope.getHeaderParams = {};
111+
scope.specs.host = '127.0.0.1:8080';
112+
var header = scope.getHeaders();
113+
114+
scope.requestModel = {
115+
scheme: "http",
116+
accept: "*/*",
117+
contentType: "multipart/form-data",
118+
parameters: {
119+
upload: './file.txt'
120+
}
121+
};
122+
var data = scope.getRequestBody()
123+
124+
sinon.stub($, "ajax").returns({
125+
fail: function() {
126+
return {done: function() {}};
127+
}
128+
});
129+
130+
scope.makeCall();
131+
132+
expect($.ajax.calledWithMatch({
133+
url: 'http://localhost:3000/upload',
134+
type: 'post',
135+
headers: header,
136+
data: data,
137+
contentType: false,
138+
processData: false
139+
}));
140+
});
141+
});
142+
57143
});
58144
});

0 commit comments

Comments
 (0)