Passed
Push — 6.4 ( 969936...be76f2 )
by Christian
44:06 queued 29:02
created

entity-validation.service.spec.js ➔ createService   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
1
import EntityValidationService from 'src/app/service/entity-validation.service';
2
import EntityFactory from 'src/core/data/entity-factory.data';
3
import ChangesetGenerator from 'src/core/data/changeset-generator.data';
4
import ErrorResolver from 'src/core/data/error-resolver.data';
5
import EntityDefinition from 'src/core/data/entity-definition.data';
6
import EntityDefinitionFactory from 'src/core/factory/entity-definition.factory';
7
// eslint-disable-next-line import/no-unresolved
8
import entitySchemaMock from 'src/../test/_mocks_/entity-schema.json';
9
10
function createService() {
11
    return new EntityValidationService(
12
        EntityDefinitionFactory,
13
        new ChangesetGenerator(),
14
        new ErrorResolver()
15
    );
16
}
17
18
const entityFactory = new EntityFactory();
19
20
describe('src/app/service/entity-validation.service.js', () => {
21
    beforeAll(() => {
22
        Object.entries(entitySchemaMock).forEach(([entityName, definitionData]) => {
23
            Shopware.EntityDefinition.add(entityName, new EntityDefinition(definitionData));
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
24
        });
25
    });
26
27
    it('should create a required shopware error with the right error code and source pointer', () => {
28
        const fieldPointer = '/0/name';
29
        const error = EntityValidationService.createRequiredError(fieldPointer);
30
31
        expect(error).toEqual({
32
            code: EntityValidationService.ERROR_CODE_REQUIRED,
33
            source: {
34
                pointer: fieldPointer,
35
            },
36
        });
37
    });
38
39
    it('should validate an empty product and report errors', () => {
40
        const service = createService();
41
        service.errorResolver.handleWriteErrors = jest.fn(() => undefined);
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
42
        const testEntity = entityFactory.create('product');
43
44
        // validate should return right result
45
        const isValid = service.validate(testEntity);
46
        expect(isValid).toBe(false);
47
48
        // found errors should match
49
        expect(service.errorResolver.handleWriteErrors.mock.calls.length).toBe(1);
50
        expect(service.errorResolver.handleWriteErrors.mock.calls[0][0].errors).toEqual([
51
            {
52
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
53
                source: { pointer: '/0/taxId' }
54
            },
55
            {
56
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
57
                source: { pointer: '/0/price' }
58
            },
59
            {
60
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
61
                source: { pointer: '/0/productNumber' }
62
            },
63
            {
64
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
65
                source: { pointer: '/0/stock' }
66
            },
67
            {
68
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
69
                source: { pointer: '/0/name' }
70
            }
71
        ]);
72
    });
73
74
    it('should validate missing price for a product', () => {
75
        const service = createService();
76
        service.errorResolver.handleWriteErrors = jest.fn(() => undefined);
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
77
        const testEntity = entityFactory.create('product');
78
        testEntity.name = 'MyProductName';
79
        testEntity.stock = 5;
80
        testEntity.productNumber = 'MyProductNumber';
81
        testEntity.taxId = 'some-tax-uuid';
82
        testEntity.price = [
83
            {
84
                gross: null,
85
                net: null,
86
            }
87
        ];
88
89
        // validate should return right result
90
        const isValid = service.validate(testEntity);
91
        expect(isValid).toBe(false);
92
93
        // found errors should match
94
        expect(service.errorResolver.handleWriteErrors.mock.calls.length).toBe(1);
95
        expect(service.errorResolver.handleWriteErrors.mock.calls[0][0].errors).toEqual([
96
            {
97
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
98
                source: { pointer: '/0/price/0/net' }
99
            },
100
            {
101
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
102
                source: { pointer: '/0/price/0/gross' }
103
            },
104
        ]);
105
    });
106
107
    it('should validate a complete product and report no errors', () => {
108
        const service = createService();
109
        service.errorResolver.handleWriteErrors = jest.fn(() => undefined);
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
110
        const testEntity = entityFactory.create('product');
111
        testEntity.name = 'MyProductName';
112
        testEntity.stock = 5;
113
        testEntity.productNumber = 'MyProductNumber';
114
        testEntity.taxId = 'some-tax-uuid';
115
        testEntity.price = [
116
            {
117
                gross: 10,
118
                net: 10,
119
            }
120
        ];
121
122
        // validate should return right result
123
        const isValid = service.validate(testEntity);
124
        expect(isValid).toBe(true);
125
126
        // found errors should match
127
        expect(service.errorResolver.handleWriteErrors.mock.calls.length).toBe(1);
128
        expect(service.errorResolver.handleWriteErrors.mock.calls[0][0].errors).toEqual([]);
129
    });
130
131
    it('should validate a product and report callback errors', () => {
132
        const service = createService();
133
        service.errorResolver.handleWriteErrors = jest.fn(() => undefined);
0 ignored issues
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
134
        const testEntity = entityFactory.create('product');
135
        testEntity.name = 'MyProductName';
136
        testEntity.stock = 5;
137
        testEntity.productNumber = 'MyProductNumber';
138
        testEntity.taxId = 'some-tax-uuid';
139
        testEntity.price = [
140
            {
141
                gross: 10,
142
                net: 10,
143
            }
144
        ];
145
146
        const customValidator = jest.fn((errors, product) => {
147
            // custom download product validation
148
            if (product.downloads === undefined || product.downloads.length < 1) {
149
                errors.push(EntityValidationService.createRequiredError('/0/downloads'));
150
            }
151
152
            return errors;
153
        });
154
155
        const expectedErrors = [
156
            {
157
                code: 'c1051bb4-d103-4f74-8988-acbcafc7fdc3',
158
                source: { pointer: '/0/downloads' }
159
            }
160
        ];
161
162
        // validate should return right result
163
        const isValid = service.validate(testEntity, customValidator);
164
        expect(isValid).toBe(false);
165
166
        // found errors should match
167
        expect(service.errorResolver.handleWriteErrors.mock.calls.length).toBe(1);
168
        expect(service.errorResolver.handleWriteErrors.mock.calls[0][0].errors).toEqual(expectedErrors);
169
170
        // custom validator should have been called with the right arguments
171
        expect(customValidator.mock.calls.length).toBe(1);
172
        expect(customValidator.mock.calls[0][0]).toEqual(expectedErrors); // initial errors already modified because of array reference
173
        expect(customValidator.mock.calls[0][1]).toBe(testEntity); // entity
174
        expect(customValidator.mock.calls[0][2]).toBe(Shopware.EntityDefinition.get(testEntity.getEntityName())); // entity definition
0 ignored issues
show
Bug introduced by
The variable Shopware seems to be never declared. If this is a global, consider adding a /** global: Shopware */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
175
        expect(customValidator.mock.results[0].value).toEqual(expectedErrors); // should return the errors
176
    });
177
});
178