Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
logging-scratch-gui
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Goessmann
logging-scratch-gui
Commits
f760e15e
Commit
f760e15e
authored
6 years ago
by
Ben Wheeler
Browse files
Options
Downloads
Patches
Plain Diff
made upload title extraction into separate testable function; tests not working yet
parent
641cfd05
Loading
Loading
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/containers/sb-file-uploader.jsx
+16
-10
16 additions, 10 deletions
src/containers/sb-file-uploader.jsx
test/unit/containers/sb-file-uploader.test.jsx
+67
-0
67 additions, 0 deletions
test/unit/containers/sb-file-uploader.test.jsx
with
83 additions
and
10 deletions
src/containers/sb-file-uploader.jsx
+
16
−
10
View file @
f760e15e
...
@@ -47,6 +47,13 @@ class SBFileUploader extends React.Component {
...
@@ -47,6 +47,13 @@ class SBFileUploader extends React.Component {
'
handleClick
'
'
handleClick
'
]);
]);
}
}
getProjectTitleFromFilename
(
fileInputFilename
)
{
if
(
!
fileInputFilename
)
return
''
;
// only parse title from files like "filename.sb2" or "filename.sb3"
const
matches
=
fileInputFilename
.
match
(
/^
(
.*
)\.
sb
[
23
]
$/
);
if
(
!
matches
)
return
''
;
return
matches
[
1
].
substring
(
0
,
100
);
// truncate project title to max 100 chars
}
// called when user has finished selecting a file to upload
// called when user has finished selecting a file to upload
handleChange
(
e
)
{
handleChange
(
e
)
{
// Remove the hash if any (without triggering a hash change event or a reload)
// Remove the hash if any (without triggering a hash change event or a reload)
...
@@ -76,15 +83,8 @@ class SBFileUploader extends React.Component {
...
@@ -76,15 +83,8 @@ class SBFileUploader extends React.Component {
if
(
thisFileInput
.
files
)
{
// Don't attempt to load if no file was selected
if
(
thisFileInput
.
files
)
{
// Don't attempt to load if no file was selected
this
.
props
.
onLoadingStarted
();
this
.
props
.
onLoadingStarted
();
reader
.
readAsArrayBuffer
(
thisFileInput
.
files
[
0
]);
reader
.
readAsArrayBuffer
(
thisFileInput
.
files
[
0
]);
// extract the title from the file and set it as current project title
const
uploadedProjectTitle
=
this
.
getProjectTitleFromFilename
(
thisFileInput
.
files
[
0
].
name
);
if
(
thisFileInput
.
files
[
0
].
name
)
{
this
.
props
.
onUpdateProjectTitle
(
uploadedProjectTitle
);
// only parse title from files like "filename.sb2" or "filename.sb3"
const
matches
=
thisFileInput
.
files
[
0
].
name
.
match
(
/^
(
.*
)\.
sb
[
23
]
$/
);
if
(
matches
)
{
const
truncatedProjectTitle
=
matches
[
1
].
substring
(
0
,
100
);
this
.
props
.
onUpdateProjectTitle
(
truncatedProjectTitle
);
}
}
}
}
}
}
handleClick
()
{
handleClick
()
{
...
@@ -137,7 +137,13 @@ const mapDispatchToProps = dispatch => ({
...
@@ -137,7 +137,13 @@ const mapDispatchToProps = dispatch => ({
}
}
});
});
// Allow incoming props to override redux-provided props. Used to mock in tests.
const
mergeProps
=
(
stateProps
,
dispatchProps
,
ownProps
)
=>
Object
.
assign
(
{},
stateProps
,
dispatchProps
,
ownProps
);
export
default
connect
(
export
default
connect
(
mapStateToProps
,
mapStateToProps
,
mapDispatchToProps
mapDispatchToProps
,
mergeProps
)(
injectIntl
(
SBFileUploader
));
)(
injectIntl
(
SBFileUploader
));
This diff is collapsed.
Click to expand it.
test/unit/containers/sb-file-uploader.test.jsx
0 → 100644
+
67
−
0
View file @
f760e15e
import
React
from
'
react
'
;
import
{
Provider
}
from
'
react-redux
'
;
import
{
mountWithIntl
,
shallowWithIntl
,
componentWithIntl
}
from
'
../../helpers/intl-helpers.jsx
'
;
import
configureStore
from
'
redux-mock-store
'
;
import
SBFileUploader
from
'
../../../src/containers/sb-file-uploader.jsx
'
;
describe
(
'
SBFileUploader
'
,
()
=>
{
const
mockStore
=
configureStore
();
let
onLoadingFinished
;
let
onLoadingStarted
;
let
onUpdateProjectTitle
;
let
store
;
// Wrap this in a function so it gets test specific states and can be reused.
const
getContainer
=
function
()
{
return
(
<
SBFileUploader
store
=
{
store
}
onLoadingFinished
=
{
onLoadingFinished
}
onLoadingStarted
=
{
onLoadingStarted
}
onUpdateProjectTitle
=
{
onUpdateProjectTitle
}
>
{
(
renderFileInput
,
loadProject
)
=>
(
<
div
onClick
=
{
loadProject
}
/>
)
}
</
SBFileUploader
>
);
};
beforeEach
(()
=>
{
store
=
mockStore
({
scratchGui
:
{
projectState
:
{}
}
});
onUpdateProjectTitle
=
jest
.
fn
();
onLoadingFinished
=
jest
.
fn
();
onLoadingStarted
=
jest
.
fn
();
});
test
(
'
correctly sets title with .sb3 filename
'
,
()
=>
{
const
component
=
mountWithIntl
(
getContainer
());
const
instance
=
component
.
instance
();
const
projectName
=
instance
.
getProjectTitleFromFilename
(
'
my project is great.sb3
'
);
expect
(
projectName
).
toBe
(
'
my project is great
'
);
});
test
(
'
correctly sets title with .sb3 filename
'
,
()
=>
{
const
component
=
componentWithIntl
(
getContainer
());
const
projectName
=
component
.
getProjectTitleFromFilename
(
'
my project is great.sb3
'
);
expect
(
projectName
).
toBe
(
'
my project is great
'
);
});
test
(
'
sets blank title with .sb filename
'
,
()
=>
{
const
component
=
componentWithIntl
(
getContainer
());
const
projectName
=
component
.
getProjectTitleFromFilename
(
'
my project is great.sb
'
);
expect
(
projectName
).
toBe
(
''
);
});
test
(
'
sets blank title with filename with no extension
'
,
()
=>
{
const
component
=
componentWithIntl
(
getContainer
());
const
projectName
=
component
.
getProjectTitleFromFilename
(
'
my project is great
'
);
expect
(
projectName
).
toBe
(
''
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment