generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
service.test.js
117 lines (105 loc) · 4.18 KB
/
service.test.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { readFile } from '@web/test-runner-commands';
import { delay } from '../src/external.js';
import { TAG_NAME_SERVICE, Defaults, init, reset } from '../src/index.js';
import { HTMLWcmsCommerceElement } from '../src//service.js';
import { mockConfig } from './mocks/config.js';
import { mockFetch } from './mocks/fetch.js';
import { mockIms, unmockIms } from './mocks/ims.js';
import { expect } from './utilities.js';
import { mockProviders } from './mocks/providers.js';
import { withWcs } from './mocks/wcs.js';
describe('commerce service', () => {
before(async () => {
await mockFetch(withWcs);
});
afterEach(() => {
reset();
unmockIms();
});
beforeEach(async () => {
await mockIms();
});
describe('function "init"', () => {
it('initialises service with configured locale', async () => {
const { settings } = await init(
mockConfig({}, { prefix: 'mena_en' }),
);
expect(settings).to.deep.contain({
country: 'DZ',
language: 'en',
});
});
it('if called witout args, returns a promise resolving when new instance is created', async () => {
let resolved = null;
init().then((instance) => {
resolved = instance;
});
await delay();
expect(resolved).to.be.null;
const instance = await init(mockConfig());
await delay();
expect(resolved).to.be.equal(instance);
});
it('returns same object for subsequent inits', async () => {
const instance = await init(mockConfig());
expect(await init()).to.be.equal(instance);
expect(await init()).to.be.equal(instance);
});
it('ignores active instance and constructs new one if argument `force` is true', async () => {
const instance = await init(mockConfig());
expect(await init(mockConfig(), mockProviders())).not.to.be.equal(
instance,
);
});
});
describe('service instance', () => {
describe('property "defaults"', () => {
it('returns "Defauls" object', async () => {
const instance = await init(mockConfig());
expect(instance.defaults).to.deep.equal(Defaults);
});
});
describe('property "literals"', () => {
it('returns "price literals" object', async () => {
const priceLiterals = await (readFile({ path: '../price-literals.json' }));
const commerce = {
priceLiterals: JSON.parse(priceLiterals),
};
const instance = await init(mockConfig(commerce));
[
'alternativePriceAriaLabel',
'freeAriaLabel',
'freeLabel',
'perUnitAriaLabel',
'perUnitLabel',
'recurrenceAriaLabel',
'recurrenceLabel',
'strikethroughAriaLabel',
'taxExclusiveLabel',
'taxInclusiveLabel',
].forEach((key) => {
const literal = instance.literals.price[key];
expect(literal).to.be.string;
expect(literal).to.not.be.empty;
});
});
});
});
describe(`component "${TAG_NAME_SERVICE}"`, () => {
it('appears inthe document head when the service activates', async () => {
let element = document.head.querySelector(TAG_NAME_SERVICE);
expect(element).to.be.null;
await init(mockConfig());
element = document.head.querySelector(TAG_NAME_SERVICE);
expect(element).to.be.instanceof(HTMLWcmsCommerceElement);
});
describe('property "isWcmsCommerce"', () => {
it('returns true', async () => {
await init(mockConfig());
const element = document.head.querySelector(TAG_NAME_SERVICE);
// @ts-ignore
expect(element.isWcmsCommerce).to.be.true;
});
});
});
});