Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit 1f34cb5

Browse files
committed
test(api): else cases for bind.ts
1 parent 39fa4c6 commit 1f34cb5

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/core/directives/__test__/bind.spec.ts

+70
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,74 @@ describe('.bindDirective', () => {
134134
expect(el.title).toEqual('test');
135135
expect(el.translate).toEqual(null);
136136
});
137+
138+
it('should accept string format for attributes', () => {
139+
const el = document.createElement('a');
140+
const expression = 'foo';
141+
const state = { foo: expression };
142+
bindDirective({
143+
el,
144+
parts: ['bind', 'title'],
145+
data: {
146+
value: expression,
147+
compute: compute(expression, el),
148+
deps: ['title'],
149+
},
150+
state,
151+
});
152+
expect(el.getAttribute('title')).toEqual('foo');
153+
});
154+
155+
it('should remove attribute if no value apparent', () => {
156+
const el = document.createElement('a');
157+
const expression = '';
158+
const state = {};
159+
bindDirective({
160+
el,
161+
parts: ['bind', 'title'],
162+
data: {
163+
value: expression,
164+
compute: compute(expression, el),
165+
deps: ['title'],
166+
},
167+
state,
168+
});
169+
expect(el.hasAttribute('title')).toEqual(false);
170+
});
171+
172+
it('should remove class attribute if no classes apparent', () => {
173+
const el = document.createElement('a');
174+
const expression = `{}`;
175+
const state = {};
176+
bindDirective({
177+
el,
178+
parts: ['bind', 'class'],
179+
data: {
180+
value: expression,
181+
compute: compute(expression, el),
182+
deps: [],
183+
},
184+
state,
185+
});
186+
expect(el.className).toEqual('');
187+
expect(el.hasAttribute('class')).toEqual(false);
188+
});
189+
190+
it('should remove style attribute if no styles apparent', () => {
191+
const el = document.createElement('a');
192+
el.setAttribute('style', '');
193+
const expression = `{}`;
194+
const state = {};
195+
bindDirective({
196+
el,
197+
parts: ['bind', 'style'],
198+
data: {
199+
value: expression,
200+
compute: compute(expression, el),
201+
deps: [],
202+
},
203+
state,
204+
});
205+
expect(el.hasAttribute('style')).toEqual(false);
206+
});
137207
});

src/core/directives/bind.ts

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const bindDirective = ({ el, parts, data, state }: DirectiveProps): void
3030
} else if (formatAcceptableWhitespace(rawClasses).length > 0) {
3131
el.className = formatAcceptableWhitespace(rawClasses);
3232
} else {
33-
/* istanbul ignore next */
3433
el.className = '';
3534
el.removeAttribute('class');
3635
}
@@ -41,7 +40,6 @@ export const bindDirective = ({ el, parts, data, state }: DirectiveProps): void
4140
case 'style': {
4241
// Accept object and set properties based on boolean state value
4342
const styles = data.compute(state);
44-
/* istanbul ignore next */
4543
if (el.hasAttribute('style')) el.removeAttribute('style');
4644
Object.entries(styles).forEach(([styleName, styleValue]) => {
4745
el.style[styleName] = styleValue;
@@ -53,7 +51,6 @@ export const bindDirective = ({ el, parts, data, state }: DirectiveProps): void
5351
const attributes = data.compute(state);
5452

5553
// Allow object syntax in binding without modifier
56-
/* istanbul ignore else */
5754
if (typeof attributes === 'object' && attributes !== null) {
5855
Object.entries(attributes).forEach(([name, value]) => {
5956
// Only set attr if not falsy

0 commit comments

Comments
 (0)