Skip to content

Commit 521117d

Browse files
committed
made upload title extraction into separate testable function; tests not working yet
1 parent ac67292 commit 521117d

File tree

2 files changed

+83
-10
lines changed

2 files changed

+83
-10
lines changed

src/containers/sb-file-uploader.jsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class SBFileUploader extends React.Component {
4747
'handleClick'
4848
]);
4949
}
50+
getProjectTitleFromFilename (fileInputFilename) {
51+
if (!fileInputFilename) return '';
52+
// only parse title from files like "filename.sb2" or "filename.sb3"
53+
const matches = fileInputFilename.match(/^(.*)\.sb[23]$/);
54+
if (!matches) return '';
55+
return matches[1].substring(0, 100); // truncate project title to max 100 chars
56+
}
5057
// called when user has finished selecting a file to upload
5158
handleChange (e) {
5259
// Remove the hash if any (without triggering a hash change event or a reload)
@@ -76,15 +83,8 @@ class SBFileUploader extends React.Component {
7683
if (thisFileInput.files) { // Don't attempt to load if no file was selected
7784
this.props.onLoadingStarted();
7885
reader.readAsArrayBuffer(thisFileInput.files[0]);
79-
// extract the title from the file and set it as current project title
80-
if (thisFileInput.files[0].name) {
81-
// only parse title from files like "filename.sb2" or "filename.sb3"
82-
const matches = thisFileInput.files[0].name.match(/^(.*)\.sb[23]$/);
83-
if (matches) {
84-
const truncatedProjectTitle = matches[1].substring(0, 100);
85-
this.props.onUpdateProjectTitle(truncatedProjectTitle);
86-
}
87-
}
86+
const uploadedProjectTitle = this.getProjectTitleFromFilename(thisFileInput.files[0].name);
87+
this.props.onUpdateProjectTitle(uploadedProjectTitle);
8888
}
8989
}
9090
handleClick () {
@@ -137,7 +137,13 @@ const mapDispatchToProps = dispatch => ({
137137
}
138138
});
139139

140+
// Allow incoming props to override redux-provided props. Used to mock in tests.
141+
const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign(
142+
{}, stateProps, dispatchProps, ownProps
143+
);
144+
140145
export default connect(
141146
mapStateToProps,
142-
mapDispatchToProps
147+
mapDispatchToProps,
148+
mergeProps
143149
)(injectIntl(SBFileUploader));
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import {Provider} from 'react-redux';
3+
import {mountWithIntl, shallowWithIntl, componentWithIntl} from '../../helpers/intl-helpers.jsx';
4+
import configureStore from 'redux-mock-store';
5+
import SBFileUploader from '../../../src/containers/sb-file-uploader.jsx';
6+
7+
describe('SBFileUploader', () => {
8+
const mockStore = configureStore();
9+
let onLoadingFinished;
10+
let onLoadingStarted;
11+
let onUpdateProjectTitle;
12+
let store;
13+
14+
// Wrap this in a function so it gets test specific states and can be reused.
15+
const getContainer = function () {
16+
return (
17+
<SBFileUploader
18+
store={store}
19+
onLoadingFinished={onLoadingFinished}
20+
onLoadingStarted={onLoadingStarted}
21+
onUpdateProjectTitle={onUpdateProjectTitle}
22+
>
23+
{(renderFileInput, loadProject) => (
24+
<div
25+
onClick={loadProject}
26+
/>
27+
)}
28+
</SBFileUploader>
29+
);
30+
};
31+
32+
beforeEach(() => {
33+
store = mockStore({
34+
scratchGui: {
35+
projectState: {}
36+
}
37+
});
38+
onUpdateProjectTitle = jest.fn();
39+
onLoadingFinished = jest.fn();
40+
onLoadingStarted = jest.fn();
41+
});
42+
43+
test('correctly sets title with .sb3 filename', () => {
44+
const component = mountWithIntl(getContainer());
45+
const instance = component.instance();
46+
const projectName = instance.getProjectTitleFromFilename('my project is great.sb3');
47+
expect(projectName).toBe('my project is great');
48+
});
49+
50+
test('correctly sets title with .sb3 filename', () => {
51+
const component = componentWithIntl(getContainer());
52+
const projectName = component.getProjectTitleFromFilename('my project is great.sb3');
53+
expect(projectName).toBe('my project is great');
54+
});
55+
56+
test('sets blank title with .sb filename', () => {
57+
const component = componentWithIntl(getContainer());
58+
const projectName = component.getProjectTitleFromFilename('my project is great.sb');
59+
expect(projectName).toBe('');
60+
});
61+
62+
test('sets blank title with filename with no extension', () => {
63+
const component = componentWithIntl(getContainer());
64+
const projectName = component.getProjectTitleFromFilename('my project is great');
65+
expect(projectName).toBe('');
66+
});
67+
});

0 commit comments

Comments
 (0)