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
2a99b84b
Commit
2a99b84b
authored
Jun 07, 2017
by
Sean Hammond
Committed by
GitHub
Jun 07, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #424 from hypothesis/extract-configFuncSettingsFrom
Extract a new configFuncSettingsFrom() function
parents
a7454216
5bce43e6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
38 deletions
+89
-38
index.js
src/annotator/config/index.js
+1
-10
settings.js
src/annotator/config/settings.js
+37
-0
index-test.js
src/annotator/config/test/index-test.js
+20
-28
settings-test.js
src/annotator/config/test/settings-test.js
+31
-0
No files found.
src/annotator/config/index.js
View file @
2a99b84b
...
...
@@ -3,8 +3,6 @@
var
settings
=
require
(
'./settings'
);
var
sharedSettings
=
require
(
'../../shared/settings'
);
var
docs
=
'https://h.readthedocs.io/en/latest/embedding.html'
;
/**
* Reads the Hypothesis configuration from the environment.
*
...
...
@@ -53,14 +51,7 @@ function configFrom(window_) {
err
);
}
// Parse config from `window.hypothesisConfig` function
if
(
window_
.
hasOwnProperty
(
'hypothesisConfig'
))
{
if
(
typeof
window_
.
hypothesisConfig
===
'function'
)
{
Object
.
assign
(
config
,
window_
.
hypothesisConfig
());
}
else
{
throw
new
TypeError
(
'hypothesisConfig must be a function, see: '
+
docs
);
}
}
Object
.
assign
(
config
,
settings
.
configFuncSettingsFrom
(
window_
));
// Convert legacy keys/values in config to corresponding current
// configuration.
...
...
src/annotator/config/settings.js
View file @
2a99b84b
...
...
@@ -70,8 +70,45 @@ function query(url) {
return
null
;
}
/**
* Return an object containing config settings from window.hypothesisConfig().
*
* Return an object containing config settings returned by the
* window.hypothesisConfig() function provided by the host page:
*
* {
* fooSetting: 'fooValue',
* barSetting: 'barValue',
* ...
* }
*
* If there's no window.hypothesisConfig() function then return {}.
*
* If there is a window.hypothesisConfig but it isn't a function then throw an
* error.
*
* @param {Window} window_ - The window to search for a hypothesisConfig() function
* @return {Object} - Any config settings returned by hypothesisConfig()
*
* @throws {TypeError} - If window.hypothesisConfig() isn't a function
*
*/
function
configFuncSettingsFrom
(
window_
)
{
if
(
!
window_
.
hasOwnProperty
(
'hypothesisConfig'
))
{
return
{};
}
if
(
typeof
window_
.
hypothesisConfig
!==
'function'
)
{
var
docs
=
'https://h.readthedocs.io/projects/client/en/latest/publishers/config/#window.hypothesisConfig'
;
throw
new
TypeError
(
'hypothesisConfig must be a function, see: '
+
docs
);
}
return
window_
.
hypothesisConfig
();
}
module
.
exports
=
{
app
:
app
,
annotations
:
annotations
,
query
:
query
,
configFuncSettingsFrom
:
configFuncSettingsFrom
,
};
src/annotator/config/test/index-test.js
View file @
2a99b84b
...
...
@@ -32,6 +32,7 @@ describe('annotator.config', function() {
fakeSettings
.
app
=
sinon
.
stub
().
returns
(
'IFRAME_URL'
);
fakeSettings
.
annotations
=
sinon
.
stub
();
fakeSettings
.
query
=
sinon
.
stub
();
fakeSettings
.
configFuncSettingsFrom
=
sinon
.
stub
().
returns
({});
});
afterEach
(
'reset the sandbox'
,
function
()
{
...
...
@@ -104,51 +105,42 @@ describe('annotator.config', function() {
});
});
context
(
"when there's a window.hypothesisConfig() function"
,
function
()
{
it
(
'reads arbitrary settings from hypothesisConfig() into config'
,
function
()
{
var
window_
=
fakeWindow
();
window_
.
hypothesisConfig
=
sinon
.
stub
().
returns
({
foo
:
'bar'
});
it
(
'gets config settings from window.hypothesisConfig()'
,
function
()
{
var
window_
=
fakeWindow
();
configFrom
(
window_
);
assert
.
calledOnce
(
fakeSettings
.
configFuncSettingsFrom
);
assert
.
calledWithExactly
(
fakeSettings
.
configFuncSettingsFrom
,
window_
);
});
context
(
'when configFuncSettingsFrom() returns an object'
,
function
()
{
it
(
'reads arbitrary settings from configFuncSettingsFrom() into config'
,
function
()
{
fakeSettings
.
configFuncSettingsFrom
.
returns
({
foo
:
'bar'
});
var
config
=
configFrom
(
window_
);
var
config
=
configFrom
(
fakeWindow
()
);
assert
.
equal
(
config
.
foo
,
'bar'
);
});
specify
(
'hypothesisConfig() settings override js-hypothesis-config ones'
,
function
()
{
var
window_
=
fakeWindow
();
window_
.
hypothesisConfig
=
sinon
.
stub
().
returns
({
fakeSettings
.
configFuncSettingsFrom
.
returns
({
foo
:
'fooFromHypothesisConfigFunc'
});
fakeSharedSettings
.
jsonConfigsFrom
.
returns
({
foo
:
'fooFromJSHypothesisConfigObj'
,
});
var
config
=
configFrom
(
window_
);
var
config
=
configFrom
(
fakeWindow
()
);
assert
.
equal
(
config
.
foo
,
'fooFromHypothesisConfigFunc'
);
});
context
(
'if hypothesisConfig() returns a non-object value'
,
function
()
{
it
(
"doesn't add anything into the config"
,
function
()
{
var
window_
=
fakeWindow
();
window_
.
hypothesisConfig
=
sinon
.
stub
().
returns
(
42
);
var
config
=
configFrom
(
window_
);
delete
config
.
app
;
// We don't care about config.app for this test.
assert
.
deepEqual
({},
config
);
});
});
});
context
(
"when window.hypothesisConfig() isn't a function"
,
function
()
{
it
(
'throws a TypeError'
,
function
()
{
var
window_
=
fakeWindow
();
window_
.
hypothesisConfig
=
'notAFunction'
;
context
(
'when configFuncSettingsFrom() throws an error'
,
function
()
{
it
(
'throws the same error'
,
function
()
{
fakeSettings
.
configFuncSettingsFrom
.
throws
(
new
TypeError
());
assert
.
throws
(
function
()
{
configFrom
(
window_
);
},
TypeError
,
'hypothesisConfig must be a function, see: https://h.readthedocs.io/en/latest/embedding.html'
);
assert
.
throws
(
function
()
{
configFrom
(
fakeWindow
());
},
TypeError
);
});
});
...
...
src/annotator/config/test/settings-test.js
View file @
2a99b84b
...
...
@@ -186,4 +186,35 @@ describe('annotation.config.settings', function() {
});
});
});
describe
(
'#configFuncSettingsFrom'
,
function
()
{
context
(
"when there's no window.hypothesisConfig() function"
,
function
()
{
it
(
'returns {}'
,
function
()
{
var
fakeWindow
=
{};
assert
.
deepEqual
(
settings
.
configFuncSettingsFrom
(
fakeWindow
),
{});
});
});
context
(
"when window.hypothesisConfig() isn't a function"
,
function
()
{
it
(
'throws an error'
,
function
()
{
var
fakeWindow
=
{
hypothesisConfig
:
42
};
assert
.
throws
(
function
()
{
settings
.
configFuncSettingsFrom
(
fakeWindow
);
},
TypeError
);
});
});
context
(
'when window.hypothesisConfig() is a function'
,
function
()
{
it
(
'returns whatever window.hypothesisConfig() returns'
,
function
()
{
// It just blindly returns whatever hypothesisConfig() returns
// (even if it's not an object).
var
fakeWindow
=
{
hypothesisConfig
:
sinon
.
stub
().
returns
(
42
)
};
assert
.
equal
(
settings
.
configFuncSettingsFrom
(
fakeWindow
),
42
);
});
});
});
});
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