Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UserCreator from '../../../client/components/user-creator.vue';
- describe('Component - User Creator', () => {
- let store;
- let actions;
- beforeEach(() => {
- actions = {
- createUser: sinon.stub().resolves(true)
- };
- const state = {
- modules: {
- users: {
- namespaced: true,
- actions
- }
- }
- };
- store = new Vuex.Store(state);
- });
- afterEach(() => {
- actions.createUser.reset();
- });
- describe('Methods', () => {
- describe('Reset Fields', () => {
- it('Should reset the fields to the initial data', done => {
- const initialData = {
- name: '',
- email: '',
- password: '',
- avatar_url: ''
- };
- const simulatedData = {
- name: 'Cristiano Ronaldo',
- email: 'cristiano@finta.pt',
- password: 'cr7+eder',
- avatar_url: 'https://reinaldo.pt/avatar.png'
- };
- const wrapper = shallow(UserCreator,
- {
- data: simulatedData,
- store
- }
- );
- expect(wrapper.data()).to.be.equal(simulatedData);
- wrapper.vm.resetFields();
- expect(wrapper.data()).to.contain(initialData);
- done();
- });
- it('When reseting to the initial data, the dialog should not close', done => {
- const initialData = {
- name: '',
- email: '',
- password: '',
- avatar_url: '',
- dialog: true
- };
- const simulatedData = {
- name: 'Cristiano Ronaldo',
- email: 'cristiano@finta.pt',
- password: 'cr7+eder',
- avatar_url: 'https://reinaldo.pt/avatar.png',
- dialog: true
- };
- const wrapper = shallow(UserCreator,
- {
- data: simulatedData,
- store
- }
- );
- expect(wrapper.data()).to.be.equal(simulatedData);
- wrapper.vm.resetFields();
- expect(wrapper.data()).to.deep.equal(initialData);
- done();
- });
- });
- describe('CreateUser', () => {
- it('Should do nothing if there is no email and password set', done => {
- const wrapper = shallow(UserCreator,
- { store }
- );
- const result = wrapper.vm.createUser();
- expect(result).to.be.false;
- // Check if the store wasn't called
- expect(actions.createUser).to.not.have.been.called;
- done();
- });
- it('Should do nothing if there is an email set, but no password', done => {
- const email = 'teste@finta.com';
- const wrapper = shallow(UserCreator,
- {
- data: {
- email
- },
- store
- }
- );
- const result = wrapper.vm.createUser();
- expect(result).to.be.false;
- expect(wrapper.data().email).to.be.equal(email);
- // Check if the store wasn't called
- expect(actions.createUser).to.not.have.been.called;
- done();
- });
- it('Should do nothing if there is a password set, but no email', done => {
- const password = 'asdasda';
- const wrapper = shallow(UserCreator,
- {
- data: {
- password
- },
- store
- }
- );
- const result = wrapper.vm.createUser();
- expect(result).to.be.false;
- expect(wrapper.data().password).to.be.equal(password);
- // Check if the store wasn't called
- expect(actions.createUser).to.not.have.been.called;
- done();
- });
- it('Should call the action if the email and password are set', done => {
- const password = 'asdasdasda';
- const email = 'teste@getfinta.com';
- const wrapper = shallow(UserCreator,
- {
- data: {
- password,
- email
- },
- store
- }
- );
- expect(wrapper.data().password).to.be.equal(password);
- expect(wrapper.data().email).to.be.equal(email);
- const promise = wrapper.vm.createUser();
- promise.then(result => {
- // Check if the store was called
- expect(actions.createUser).to.have.been.called;
- done();
- }).catch(done);
- });
- it('Should reset the fields changed and the dialog if the user was created', done => {
- const password = 'asdasdasda';
- const email = 'teste@getfinta.com';
- const wrapper = shallow(UserCreator,
- {
- data: {
- password,
- email
- },
- store
- }
- );
- wrapper.data().dialog = true;
- expect(wrapper.data().password).to.be.equal(password);
- expect(wrapper.data().email).to.be.equal(email);
- const promise = wrapper.vm.createUser();
- // Create the spy
- const spy = sinon.spy(wrapper.vm, 'resetFields');
- promise.then(result => {
- // Check if the spy was called
- expect(wrapper.data().dialog).to.be.equal(false);
- expect(spy).to.have.been.called;
- done();
- }).catch(done);
- });
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement