Skip to content

Commit 5d91d2d

Browse files
committed
Test 'to have attributes' with array argument
1 parent 8abcb6f commit 5d91d2d

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module.exports = {
7575
if (typeof cmp === 'string') {
7676
expect(attrs, '[not] to [only] have keys', Array.prototype.slice.call(arguments, 2));
7777
} else if (Array.isArray(cmp)) {
78-
expect(attrs, '[not] to have keys', cmp);
78+
expect(attrs, '[not] to [only] have keys', cmp);
7979
}
8080
});
8181
}

test/unexpected-dom.js

+56-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('unexpected-dom', function () {
5353
expect(function () {
5454
expect(el, 'to only have attributes', 'id');
5555
}, 'to throw exception', function (err) {
56-
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes \'id\'');
56+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes \'id\'');
5757
});
5858
});
5959

@@ -70,7 +70,7 @@ describe('unexpected-dom', function () {
7070
expect(function () {
7171
expect(el, 'to have attributes', 'id', 'foo');
7272
}, 'to throw exception', function (err) {
73-
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes \'id\', \'foo\'');
73+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes \'id\', \'foo\'');
7474
});
7575
});
7676

@@ -87,7 +87,60 @@ describe('unexpected-dom', function () {
8787
expect(function () {
8888
expect(el, 'not to have attributes', 'id');
8989
}, 'to throw exception', function (err) {
90-
expect(err.output.toString(), 'to match', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> not to have attributes \'id\'');
90+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> not to have attributes \'id\'');
91+
});
92+
});
93+
});
94+
95+
describe('array comparison', function () {
96+
it('should match exact arguments', function () {
97+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
98+
99+
expect(this.body.firstChild, 'to only have attributes', ['id', 'class', 'data-info', 'disabled']);
100+
});
101+
102+
it('should fail on exact arguments not met', function () {
103+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
104+
var el = this.body.firstChild;
105+
106+
expect(function () {
107+
expect(el, 'to only have attributes', ['id']);
108+
}, 'to throw exception', function (err) {
109+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to only have attributes [ \'id\' ]');
110+
});
111+
});
112+
113+
it('should match partial arguments', function () {
114+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
115+
116+
expect(this.body.firstChild, 'to have attributes', ['id', 'class']);
117+
});
118+
119+
it('should fail on partial arguments not met', function () {
120+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
121+
var el = this.body.firstChild;
122+
123+
expect(function () {
124+
expect(el, 'to have attributes', ['id', 'foo']);
125+
}, 'to throw exception', function (err) {
126+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> to have attributes [ \'id\', \'foo\' ]');
127+
});
128+
});
129+
130+
it('should match negative arguments', function () {
131+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
132+
133+
expect(this.body.firstChild, 'not to have attributes', ['foo', 'bar']);
134+
});
135+
136+
it('should fail on negative partial arguments met', function () {
137+
this.body.innerHTML = '<button id="foo" class="bar" data-info="baz" disabled>Press me</button>';
138+
var el = this.body.firstChild;
139+
140+
expect(function () {
141+
expect(el, 'not to have attributes', ['id']);
142+
}, 'to throw exception', function (err) {
143+
expect(err.output.toString(), 'to be', 'expected <button class="bar" data-info="baz" disabled="" id="foo"/> not to have attributes [ \'id\' ]');
91144
});
92145
});
93146
});

0 commit comments

Comments
 (0)