You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{prettybytes}from'src/filters'describe('Filter "prettybytes".',()=>{it('Prettybytes shows the right KB, MB, GB, TB unit.',()=>{expect(prettybytes(1024)).to.equal('1 KB');expect(prettybytes(1048576)).to.equal('1 MB');expect(prettybytes(1048576*1024)).to.equal('1 GB');expect(prettybytes(1048576*1048576)).to.equal('1 TB');expect(prettybytes(230)).to.equal('230 Bytes');})})
Testing an API call, mocking fetch
import{authentify}from'src/store/actions/auth'importfetchMockfrom'fetch-mock'import{fetchUserToken}from'src/api/web'describe('Authentify action',()=>{letsandboxbeforeEach()=>{
sandbox =sinon.sandbox.create()}afterEach()=>{sandbox.restore()fetchMock.restore()}it('FetchUserToken should eventually resolve with a token',()=>{fetchMock.mock('/login/getaccesstoken',{Model: '?????????????'})returnfetchUserToken().shouldeventuallyequal('?????????????')})})
Testing a component
importVuefrom'vue'importFinalCountdownfrom'src/components/workflow/FinalCountdown'describe('workflow/FinalCoundDown component.'),()=>{constgetComponent=(date)=>{letvm=newVue({template: '<div><final-countdown v-ref:component :date="date"></final-countdown></div>',components: {
FinalCountdown
},data: {
date
}})returnvm}it('Should render correctly with a date in the future.',()=>{consttomorrow=newDate(newDate().getTime()+24*60*60*1000)constvm=getComponent(tomorrow).$mount()expect(vm.$el).to.be.ok//Counter should show 23:59:59//timeUntilExpiration is a component methodexpect(vm.$refs.component.timeUntilExpiration()).to.equal('23:59:59')//Priority should be normalexpect(vm.$refs.component.priorityLevel()).to.equal('normal')})}
Testing a component property change
it('Should be valid only when a radio option is selected',()=>{constvm=getComponent('Bob').$mount()constcomponent=vm.$refs.componentexpect(component.isFormValid).to.be.falsecomponent.picked=3component.$nextTick(()=>{expect(component.isFormValid).to.be.true})})
Testing a component event
it('Send should dispatch "declineWorkflowTask" with the choice payload',()=>{constvm=getComponent('Bob').$mount()constcomponent=vm.$refs.componentletspy=sinon.spy()vm.$on('declinedWorkflowTask',spy)component.sendReply()expect(spy).to.have.been.calledWith({choice: null,text: ''})})
Testing a component ready() hook
it('Should focus on textarea when ready.',(done)=>{constvm=getComponent()constcomponent=vm.$refs.component//Action triggered by Message Template child componentfetchMock.mock('/tutor/templates',[{title: 'template A',body: 'AAA';},{title: 'template B',body: 'BBB'}])//Triggers ready() on our componentvm.$appendTo(document.body)component.$nextTick(()=>{expect(document.activeElement).to.equal(component.$els.textarea)done()})})
Testing with a Vuex store
importstorefrom'src/store'describe('workflow/WorkflowAccept component',()=>{//Update store states here or import from fixturesstore.state.availability={ ... ]constgetComponent=()=>{letvm=newVue({
template ='<div><workflow-accept v-ref:component></workflow-accept</div>',components: {
WorkflowAccept
},//Inject store herestore: store})returnvm.$mount()}})
Testing a Vue action
import{sendInitialDirectContactResponse}from'src/store/actions/workflow'importstorefrom'src/store'conststate=store.stateimport{testAction}from'./helper'describe('sendInitialDirectContactResponse action.',()=>{beforeEach(()=>{state.workflow.workflowTask={id: 1conversationThread: {id: 2},initialDirectContact: {id: 3}}})it('Should dispatch the right mutations.',(done)=>{testAction(sendInitialDirectContactResponse,[1,1,'test'],state,[{name: 'DELETE_WORKFLOW_TASK',payload: [1]},{name: 'ADD_CONVERSATION_THREAD_TO_TOP'},{name: 'UPDATE_CONVERSATION_THREAD'}],done)})})it('Should not dispatch ADD_CONVERSATION_THREAD_TO_TOP if ...',(done)=>{state.app.menuSelection='archived'testAction(SendInitialDirectContactResponse,[1,1,'test'],state,[{name: 'DELETE_WORKFLOW_TASK',payload: [1]}],done)})
Testing a Vuex action - testaction.js
//Helper for testing action with expected mutationsexportfunctiontestAction(action,args,state,expectedMutations,done){letcount=0//Mock dispatchconstdispatch=(name, ...payload)=>{constmutation=expectedMutations[count]expect(mutation.name).to.equal(name)//if our mutation has a payload and our expected mutation//wants us to assert this payloadif(payload&&mutation.payload){expect(mutation.payload).to.deep.equal(payload)}count++if(count===expectedMutations.length){done()}if(count>expectedMutations.length){//Missing non expected mutations.//list all expected mutations!expect(count).to.equal(expectedMutations.length)}}//call the action with mocked store and argumentsaction({dispatch, state}, ...args)//check if no mutations should have been dispatchedif(count===0){expect(expectedMutations.length).to.equal(0)done()}}