Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
e0303a31
Unverified
Commit
e0303a31
authored
May 26, 2017
by
Sean Hammond
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve shared/settings.js tests
Add a new set of unit tests that test more cases.
parent
6eccb5de
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
116 additions
and
24 deletions
+116
-24
settings-test.js
src/shared/test/settings-test.js
+116
-24
No files found.
src/shared/test/settings-test.js
View file @
e0303a31
...
...
@@ -2,36 +2,128 @@
var
settings
=
require
(
'../settings'
);
function
createConfigElement
(
obj
)
{
var
el
=
document
.
createElement
(
'script'
);
describe
(
'settings'
,
function
()
{
describe
(
'#jsonConfigsFrom'
,
function
()
{
var
jsonConfigsFrom
=
settings
.
jsonConfigsFrom
;
function
appendJSHypothesisConfig
(
document_
,
jsonString
)
{
var
el
=
document_
.
createElement
(
'script'
);
el
.
type
=
'application/json'
;
el
.
textContent
=
JSON
.
stringify
(
obj
)
;
el
.
textContent
=
jsonString
;
el
.
classList
.
add
(
'js-hypothesis-config'
);
el
.
classList
.
add
(
'js-settings-test'
);
return
el
;
}
document_
.
body
.
appendChild
(
el
)
;
}
function
removeJSONScriptTags
()
{
afterEach
(
'remove js-hypothesis-config tags'
,
function
()
{
var
elements
=
document
.
querySelectorAll
(
'.js-settings-test'
);
for
(
var
i
=
0
;
i
<
elements
.
length
;
i
++
)
{
elements
[
i
].
parentNode
.
removeChild
(
elements
[
i
]);
}
}
});
describe
(
'settings'
,
function
()
{
describe
(
'#jsonConfigsFrom'
,
function
()
{
var
jsonConfigsFrom
=
settings
.
jsonConfigsFrom
;
afterEach
(
removeJSONScriptTags
);
context
(
'when there are no JSON scripts'
,
function
()
{
it
(
'returns {}'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{});
});
});
context
(
"when there's JSON scripts with no top-level objects"
,
function
()
{
beforeEach
(
'add JSON scripts with no top-level objects'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'null'
);
appendJSHypothesisConfig
(
document
,
'23'
);
appendJSHypothesisConfig
(
document
,
'true'
);
});
it
(
'ignores them'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{});
});
});
context
(
"when there's a JSON script with a top-level array"
,
function
()
{
beforeEach
(
'add a JSON script containing a top-level array'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'["a", "b", "c"]'
);
});
it
(
'reads config from .js-hypothesis-config <script> tags'
,
function
()
{
document
.
body
.
appendChild
(
createConfigElement
({
key
:
'value'
}));
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
key
:
'value'
});
it
(
'returns the array, parsed into an object'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
0
:
'a'
,
1
:
'b'
,
2
:
'c'
}
);
});
});
context
(
"when there's a JSON script with a top-level string"
,
function
()
{
beforeEach
(
'add a JSON script with a top-level string'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'"hi"'
);
});
it
(
'returns the string, parsed into an object'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
0
:
'h'
,
1
:
'i'
});
});
});
it
(
'merges settings from all config <script> tags'
,
function
()
{
document
.
body
.
appendChild
(
createConfigElement
({
a
:
1
}));
document
.
body
.
appendChild
(
createConfigElement
({
b
:
2
}));
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
a
:
1
,
b
:
2
});
context
(
"when there's a JSON script containing invalid JSON"
,
function
()
{
beforeEach
(
'add a JSON script containing invalid JSON'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'this is not valid json'
);
});
it
(
'throws a SyntaxError'
,
function
()
{
assert
.
throws
(
function
()
{
jsonConfigsFrom
(
document
);
},
SyntaxError
);
});
});
context
(
"when there's a JSON script with an empty object"
,
function
()
{
beforeEach
(
'add a JSON script containing an empty object'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'{}'
);
});
it
(
'ignores it'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{});
});
});
context
(
"when there's a JSON script containing some settings"
,
function
()
{
beforeEach
(
'add a JSON script containing some settings'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'{"foo": "FOO", "bar": "BAR"}'
);
});
it
(
'returns the settings'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
foo
:
'FOO'
,
bar
:
'BAR'
}
);
});
});
context
(
'when there are JSON scripts with different settings'
,
function
()
{
beforeEach
(
'add some JSON scripts with different settings'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'{"foo": "FOO"}'
);
appendJSHypothesisConfig
(
document
,
'{"bar": "BAR"}'
);
appendJSHypothesisConfig
(
document
,
'{"gar": "GAR"}'
);
});
it
(
'merges them all into one returned object'
,
function
()
{
assert
.
deepEqual
(
jsonConfigsFrom
(
document
),
{
foo
:
'FOO'
,
bar
:
'BAR'
,
gar
:
'GAR'
}
);
});
});
context
(
'when multiple JSON scripts contain the same setting'
,
function
()
{
beforeEach
(
'add some JSON scripts with different settings'
,
function
()
{
appendJSHypothesisConfig
(
document
,
'{"foo": "first"}'
);
appendJSHypothesisConfig
(
document
,
'{"foo": "second"}'
);
appendJSHypothesisConfig
(
document
,
'{"foo": "third"}'
);
});
specify
(
'settings from later in the page override ones from earlier'
,
function
()
{
assert
.
equal
(
jsonConfigsFrom
(
document
).
foo
,
'third'
);
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment