Skip to content

Commit a2aec9d

Browse files
committed
Also implement style checks in 'to only have attributes'
1 parent bf1f49b commit a2aec9d

File tree

2 files changed

+119
-20
lines changed

2 files changed

+119
-20
lines changed

lib/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ module.exports = {
380380
} else {
381381
cmpStyles = cmp.style;
382382
}
383+
384+
if (this.flags.exhaustively) {
385+
comparator.style = expect.it('to exhaustively satisfy', cmpStyles);
386+
} else {
387+
comparator.style = expect.it('to satisfy', cmpStyles);
388+
}
383389
}
384390

385391
if (this.flags.exhaustively) {
@@ -407,10 +413,6 @@ module.exports = {
407413
comparator['class'] = expect.it.apply(null, ['to contain'].concat(cmpClasses));
408414
}
409415

410-
if (cmp.style) {
411-
comparator.style = expect.it('to satisfy', cmpStyles);
412-
}
413-
414416
return expect(attrs, 'to satisfy', comparator);
415417
}
416418
} else {

test/unexpected-dom.js

+113-16
Original file line numberDiff line numberDiff line change
@@ -308,30 +308,127 @@ describe('unexpected-dom', function () {
308308
});
309309

310310
describe('style attribute', function () {
311-
it('should do string comparisons', function () {
312-
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
311+
describe('lax comparison', function () {
312+
it('should do string comparisons', function () {
313+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
313314

314-
expect(this.body.firstChild, 'to have attributes', {
315-
style: 'color: red; background: blue'
315+
expect(this.body.firstChild, 'to have attributes', {
316+
style: 'color: red; background: blue'
317+
});
316318
});
317-
});
318319

319-
it('should do string comparisons in any order', function () {
320-
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
320+
it('should do string comparisons in any order', function () {
321+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
321322

322-
expect(this.body.firstChild, 'to have attributes', {
323-
style: 'background: blue; color: red'
323+
expect(this.body.firstChild, 'to have attributes', {
324+
style: 'background: blue; color: red'
325+
});
326+
});
327+
328+
it('should do string comparisons on partial values', function () {
329+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
330+
331+
expect(this.body.firstChild, 'to have attributes', {
332+
style: 'background: blue'
333+
});
334+
});
335+
336+
it('should fail when styles are missing', function () {
337+
this.body.innerHTML = '<i style="color: red"></i>';
338+
var node = this.body.firstChild;
339+
340+
expect(function () {
341+
expect(node, 'to have attributes', {
342+
style: 'background: blue'
343+
});
344+
}, 'to throw', /to have attributes \{ style: 'background: blue' \}/);
345+
});
346+
347+
it('should do object comparisons', function () {
348+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
349+
350+
expect(this.body.firstChild, 'to have attributes', {
351+
style: {
352+
color: 'red',
353+
background: 'blue'
354+
}
355+
});
356+
});
357+
358+
it('should do partial object comparisons', function () {
359+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
360+
361+
expect(this.body.firstChild, 'to have attributes', {
362+
style: {
363+
background: 'blue'
364+
}
365+
});
366+
});
367+
368+
it('should fail on missing partial object comparisons', function () {
369+
this.body.innerHTML = '<i style="color: red"></i>';
370+
var node = this.body.firstChild;
371+
372+
expect(function () {
373+
expect(node, 'to have attributes', {
374+
style: {
375+
background: 'blue'
376+
}
377+
});
378+
}, 'to throw', /to have attributes \{ style: \{ background: 'blue' \} \}/);
324379
});
325380
});
326381

327-
it('should do object comparisons', function () {
328-
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
382+
describe('strict comparison', function () {
383+
it('should do string comparisons', function () {
384+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
329385

330-
expect(this.body.firstChild, 'to have attributes', {
331-
style: {
332-
color: 'red',
333-
background: 'blue'
334-
}
386+
expect(this.body.firstChild, 'to only have attributes', {
387+
style: 'color: red; background: blue'
388+
});
389+
});
390+
391+
it('should do string comparisons in any order', function () {
392+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
393+
394+
expect(this.body.firstChild, 'to only have attributes', {
395+
style: 'background: blue; color: red'
396+
});
397+
});
398+
399+
it('should fail when styles are missing', function () {
400+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
401+
var node = this.body.firstChild;
402+
403+
expect(function () {
404+
expect(node, 'to only have attributes', {
405+
style: 'background: blue'
406+
});
407+
}, 'to throw', /to only have attributes \{ style: 'background: blue' \}/);
408+
});
409+
410+
it('should do object comparisons', function () {
411+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
412+
413+
expect(this.body.firstChild, 'to only have attributes', {
414+
style: {
415+
color: 'red',
416+
background: 'blue'
417+
}
418+
});
419+
});
420+
421+
it('should fail on missing partial object comparisons', function () {
422+
this.body.innerHTML = '<i style="color: red; background: blue"></i>';
423+
var node = this.body.firstChild;
424+
425+
expect(function () {
426+
expect(node, 'to only have attributes', {
427+
style: {
428+
background: 'blue'
429+
}
430+
});
431+
}, 'to throw', /to only have attributes \{ style: \{ background: 'blue' \} \}/);
335432
});
336433
});
337434
});

0 commit comments

Comments
 (0)