Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit f53e3e0

Browse files
committed
MAGETWO-89675: Build Stabilization
Add product-ui-select JSUnit tests
1 parent 02bfda2 commit f53e3e0

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

app/code/Magento/Catalog/view/adminhtml/web/js/components/product-ui-select.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ define([
2121
initialize: function () {
2222
this._super();
2323

24+
this.validateInitialValue();
25+
26+
return this;
27+
},
28+
29+
/**
30+
* Validate initial value actually exists
31+
*/
32+
validateInitialValue: function () {
2433
if (this.value()) {
2534
$.ajax({
2635
url: this.validationUrl,
@@ -53,8 +62,6 @@ define([
5362
} else {
5463
this.validationLoading(false);
5564
}
56-
57-
return this;
5865
},
5966

6067
/** @inheritdoc */
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'underscore',
8+
'uiRegistry',
9+
'Magento_Catalog/js/components/product-ui-select',
10+
'ko',
11+
'jquery'
12+
], function (_, registry, Constr, ko, $) {
13+
'use strict';
14+
15+
describe('Magento_Catalog/js/components/product-ui-select', function () {
16+
var obj,
17+
jQueryMethodsOverridden = {};
18+
19+
beforeEach(function () {
20+
obj = new Constr({
21+
name: 'productUiSelect',
22+
dataScope: '',
23+
provider: 'provider',
24+
options: [],
25+
value: ''
26+
});
27+
28+
jQueryMethodsOverridden = {};
29+
});
30+
31+
afterEach(function () {
32+
_.each(jQueryMethodsOverridden, function (value, key) {
33+
$.fn[key] = value;
34+
});
35+
});
36+
37+
describe('"validateInitialValue" method', function () {
38+
it('Should be defined', function () {
39+
expect(obj.hasOwnProperty('validateInitialValue')).toBeDefined();
40+
});
41+
42+
it('Should call not call ajax if value is empty', function () {
43+
jQueryMethodsOverridden.ajax = $.ajax;
44+
spyOn($, 'ajax');
45+
46+
spyOn(obj, 'validationLoading');
47+
spyOn(obj, 'value').and.returnValue('');
48+
49+
expect(obj.validateInitialValue()).toBeUndefined();
50+
51+
expect($.ajax).not.toHaveBeenCalled();
52+
expect(obj.validationLoading).toHaveBeenCalledWith(false);
53+
});
54+
55+
it('Should call ajax if value is not empty', function () {
56+
var successCallback,
57+
completeCallback,
58+
responseData = {
59+
label: 'hello world',
60+
value: 'hello world'
61+
};
62+
63+
jQueryMethodsOverridden.ajax = $.ajax;
64+
spyOn($, 'ajax').and.callFake(function (request) {
65+
successCallback = request.success.bind(obj);
66+
completeCallback = request.complete.bind(obj);
67+
});
68+
69+
spyOn(obj, 'validationLoading');
70+
spyOn(obj, 'value').and.returnValue('hello world');
71+
spyOn(obj, 'options');
72+
spyOn(obj, 'setCaption');
73+
74+
expect(obj.validateInitialValue()).toBeUndefined();
75+
76+
successCallback(responseData);
77+
completeCallback();
78+
79+
expect($.ajax).toHaveBeenCalled();
80+
81+
expect(obj.validationLoading).toHaveBeenCalledWith(false);
82+
expect(obj.loadedOption).toBe(responseData);
83+
expect(obj.options).toHaveBeenCalledWith([responseData]);
84+
85+
expect(obj.setCaption).toHaveBeenCalled();
86+
});
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)