-
Notifications
You must be signed in to change notification settings - Fork 23
/
dom.nested.test.js
159 lines (143 loc) · 4.77 KB
/
dom.nested.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import assert from 'assert';
import React from 'react';
import TestUtils from 'react-dom/test-utils';
import DocumentMeta from '../';
import { removeDocumentMeta } from '../dom';
import { getElements, getAttr } from './test-utils';
const document = global.document;
describe('DocumentMeta - DOM nested', () => {
const DOC_META = {
title: 'This is a document title',
description: 'This meta value is describing the page we are looking at',
canonical: 'http://domain.tld/path/to/page',
meta: {
charset: 'utf-8',
name: {
keywords: 'react,document,meta,tags'
}
},
link: {
rel: {
stylesheet: [
'http://domain.tld/css/vendor.css',
'http://domain.tld/css/styles.css'
]
}
}
};
const DOC_META_NESTED = {
title: 'This is another document title',
description: null,
canonical: 'http://domain.tld/path/to/other',
meta: {
name: {
keywords: 'react,document,meta,tags,nesting'
}
},
link: {
rel: {}
}
};
describe('Basic nested', () => {
beforeEach(() => {
DocumentMeta.canUseDOM = true;
removeDocumentMeta();
TestUtils.renderIntoDocument(
<div>
<DocumentMeta {...DOC_META} />
<div>
<DocumentMeta {...DOC_META_NESTED} extend />
</div>
</div>
);
});
it('should render document.title / <title> according to the nested title-prop', () => {
assert.strictEqual(document.title, DOC_META_NESTED.title);
});
it('should render <meta name="description" content="..."> according to the nested description-prop', () => {
assert.strictEqual(
getAttr('meta[name=description]', 'content'),
DOC_META_NESTED.description
);
});
it('should render <link rel="canonical" href="..." according to the nested canonical-prop', () => {
assert.strictEqual(
getAttr('link[rel=canonical]', 'href'),
DOC_META_NESTED.canonical
);
});
it('should render simple meta tags, eg. <meta charset="...">', () => {
assert.strictEqual(
getAttr('meta[charset]', 'charset'),
DOC_META.meta.charset
);
});
it('should render normal meta tags, eg. <meta name="..." content="...">', () => {
Object.keys(DOC_META.meta.name).forEach(name => {
const value = DOC_META_NESTED.meta.name.hasOwnProperty(name)
? DOC_META_NESTED.meta.name[name]
: DOC_META.meta.name[name];
assert.strictEqual(
getAttr(`meta[name=${name}]`, 'content'),
value,
`<meta name="${name}" ... /> has not been rendered correctly`
);
});
});
it('should render normal link tags, eg. <link rel="..." href="...">', () => {
Object.keys(DOC_META.link.rel).forEach(rel => {
const value = DOC_META_NESTED.link.rel.hasOwnProperty(rel)
? DOC_META_NESTED.link.rel[rel]
: DOC_META.link.rel[rel];
const values = Array.isArray(value) ? value : [value];
let idx = 0;
const elements = getElements(`link[rel=${rel}]`);
for (const element of elements) {
assert.strictEqual(
element.getAttribute('href'),
values[idx++],
`<link rel="${rel}" ... /> has not been rendered correctly`
);
}
});
});
});
it('keep document.title if none is provided to DocumentMeta', () => {
DocumentMeta.canUseDOM = true;
removeDocumentMeta();
document.title = 'Static document title';
TestUtils.renderIntoDocument(<DocumentMeta />);
assert.strictEqual(document.title, 'Static document title');
TestUtils.renderIntoDocument(<DocumentMeta title="Dynamic document title" />);
assert.strictEqual(document.title, 'Dynamic document title');
});
describe('Deep nesting', () => {
beforeEach(() => {
DocumentMeta.canUseDOM = true;
removeDocumentMeta();
TestUtils.renderIntoDocument(
<DocumentMeta meta={{ name: { l1: 'a' } }}>
<DocumentMeta meta={{ name: { l2: 'b' } }} extend>
<DocumentMeta meta={{ name: { l3: 'c' } }}>
<DocumentMeta meta={{ name: { l4: 'd' } }} extend />
</DocumentMeta>
</DocumentMeta>
</DocumentMeta>
);
});
it('should render inside-out, but only as long as the parent component is extendable', () => {
const expected = { l4: 'd', l3: 'c' };
const actual = {};
const elements = getElements(`meta[name]`);
for (const element of elements) {
const name = element.getAttribute('name');
actual[name] = element.getAttribute('content');
}
assert.deepEqual(
expected,
actual,
`<meta name="..." content="..." /> has not been rendered correctly`
);
});
});
});