|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +define([ |
| 7 | + 'Magento_ConfigurableProduct/js/variations/variations' |
| 8 | +], function (Variations) { |
| 9 | + 'use strict'; |
| 10 | + |
| 11 | + describe('Magento_ConfigurableProduct/js/variations/variations', function () { |
| 12 | + var variation; |
| 13 | + |
| 14 | + beforeEach(function () { |
| 15 | + variation = new Variations(); |
| 16 | + |
| 17 | + variation.source = { |
| 18 | + data: {} |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + it('checks that "serializeData" serializes data', function () { |
| 23 | + var matrix = [ |
| 24 | + { |
| 25 | + name: 'Product1', |
| 26 | + attributes: 'Color: black', |
| 27 | + price: 100 |
| 28 | + }, |
| 29 | + { |
| 30 | + name: 'Product2', |
| 31 | + attributes: 'Color: red', |
| 32 | + price: 50 |
| 33 | + }, |
| 34 | + { |
| 35 | + name: 'Product3', |
| 36 | + attributes: 'Color: white', |
| 37 | + price: 20 |
| 38 | + } |
| 39 | + ], |
| 40 | + ids = [1, 2, 3], |
| 41 | + resultMatrix = JSON.stringify(matrix), |
| 42 | + resultIds = JSON.stringify(ids); |
| 43 | + |
| 44 | + variation.source.data['configurable-matrix'] = matrix; |
| 45 | + variation.source.data['associated_product_ids'] = ids; |
| 46 | + |
| 47 | + variation.serializeData(); |
| 48 | + |
| 49 | + expect(variation.source.data['configurable-matrix']).toBeUndefined(); |
| 50 | + expect(variation.source.data['associated_product_ids']).toBeUndefined(); |
| 51 | + expect(variation.source.data['configurable-matrix-serialized']).toEqual(resultMatrix); |
| 52 | + expect(variation.source.data['associated_product_ids_serialized']).toEqual(resultIds); |
| 53 | + }); |
| 54 | + |
| 55 | + it('checks that "serializeData" uses old data if there is no data to serialize', function () { |
| 56 | + |
| 57 | + var matrix = [ |
| 58 | + { |
| 59 | + name: 'Product4', |
| 60 | + attributes: 'Color: grey', |
| 61 | + price: 5 |
| 62 | + }, |
| 63 | + { |
| 64 | + name: 'Product5', |
| 65 | + attributes: 'Color: pink', |
| 66 | + price: 70 |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'Product6', |
| 70 | + attributes: 'Color: brown', |
| 71 | + price: 30 |
| 72 | + } |
| 73 | + ], |
| 74 | + ids = [4, 5, 6], |
| 75 | + resultMatrix = JSON.stringify(matrix), |
| 76 | + resultIds = JSON.stringify(ids); |
| 77 | + |
| 78 | + variation.source.data['configurable-matrix-serialized'] = JSON.stringify(matrix); |
| 79 | + variation.source.data['associated_product_ids_serialized'] = JSON.stringify(ids); |
| 80 | + |
| 81 | + variation.serializeData(); |
| 82 | + |
| 83 | + expect(variation.source.data['configurable-matrix-serialized']).toEqual(resultMatrix); |
| 84 | + expect(variation.source.data['associated_product_ids_serialized']).toEqual(resultIds); |
| 85 | + }); |
| 86 | + |
| 87 | + it('checks that "serializeData" works correctly if we have new data to be serialized', function () { |
| 88 | + var matrix = [ |
| 89 | + { |
| 90 | + name: 'Product7', |
| 91 | + attributes: 'Color: yellow', |
| 92 | + price: 10 |
| 93 | + }, |
| 94 | + { |
| 95 | + name: 'Product8', |
| 96 | + attributes: 'Color: green', |
| 97 | + price: 200 |
| 98 | + }, |
| 99 | + { |
| 100 | + name: 'Product9', |
| 101 | + attributes: 'Color: blue', |
| 102 | + price: 500 |
| 103 | + } |
| 104 | + ], |
| 105 | + ids = [7, 8, 9], |
| 106 | + resultMatrix = JSON.stringify(matrix), |
| 107 | + resultIds = JSON.stringify(ids); |
| 108 | + |
| 109 | + variation.source.data['configurable-matrix'] = matrix; |
| 110 | + variation.source.data['associated_product_ids'] = ids; |
| 111 | + variation.source.data['configurable-matrix-serialized'] = JSON.stringify(['some old data']); |
| 112 | + variation.source.data['associated_product_ids_serialized'] = JSON.stringify(['some old data']); |
| 113 | + variation.serializeData(); |
| 114 | + |
| 115 | + expect(variation.source.data['configurable-matrix']).toBeUndefined(); |
| 116 | + expect(variation.source.data['associated_product_ids']).toBeUndefined(); |
| 117 | + expect(variation.source.data['configurable-matrix-serialized']).toEqual(resultMatrix); |
| 118 | + expect(variation.source.data['associated_product_ids_serialized']).toEqual(resultIds); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments