Last active
July 6, 2017 10:09
-
-
Save ernestofreyreg/1d1482aff730d4d8c9ed9f45d50f8f2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import expect from 'must' | |
import sinon from 'sinon' | |
import mustSinon from 'must-sinon' | |
import { | |
uploadFileReducer, | |
createActions, | |
initialState, | |
INIT_UPLOADER, | |
START_UPLOADING | |
} from '../../../components/FileUploader/uploader' | |
mustSinon(expect) | |
describe('uploader', () => { | |
const fakeFile = { | |
name: 'somefile.ext', | |
type: 'mime/type', | |
size: 1000 | |
} | |
const fakeTokenSource = { | |
cancel: sinon.stub() | |
} | |
beforeEach(() => { | |
fakeTokenSource.cancel.reset() | |
}) | |
describe('reducer', () => { | |
it(INIT_UPLOADER, () => { | |
const state = uploadFileReducer(null, {type: INIT_UPLOADER}) | |
state.must.be.eql(initialState) | |
}) | |
it(START_UPLOADING, () => { | |
const state = uploadFileReducer( | |
initialState, | |
{ | |
type: START_UPLOADING, | |
uploadingFile: fakeFile, | |
cancelTokenSource: fakeTokenSource | |
} | |
) | |
state.must.have.property('uploading', true) | |
state.uploadingFile.must.be.eql(fakeFile) | |
state.uploadProgress.must.be.eql({size: fakeFile.size, sent: 0}) | |
state.cancelTokenSource.must.be.eql(fakeTokenSource) | |
}) | |
// TODO: Reminder of actions types | |
}) | |
describe('actions', () => { | |
function mocks (initialState) { | |
const mockAttach = () => { | |
let state = initialState | |
function setState(fn, cb) { | |
state = fn(state) | |
cb() | |
} | |
return { | |
get: () => state, | |
set: fn => new Promise(resolve => setState(fn, resolve)) | |
} | |
} | |
const instance = mockAttach() | |
const actions = createActions(instance) | |
return { | |
instance, | |
actions | |
} | |
} | |
describe('cancelUpload', () => { | |
it('is completed', () => { | |
const {instance, actions} = mocks({...initialState, completed: true}) | |
return actions.cancelUpload() | |
.then(() => { | |
instance.get().completed.must.be.false() | |
}) | |
}) | |
it('is uploading', () => { | |
const {instance, actions} = mocks({...initialState, uploading: true, cancelTokenSource: fakeTokenSource}) | |
return actions.cancelUpload() | |
.then(() => { | |
fakeTokenSource.cancel.must.have.been.called() | |
}) | |
}) | |
}) | |
// TODO: Reminder of actions | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment