Advertisement
Guest User

Untitled

a guest
Oct 7th, 2017
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UserCreator from '../../../client/components/user-creator.vue';
  2.  
  3. describe('Component - User Creator', () => {
  4.   let store;
  5.   let actions;
  6.  
  7.   beforeEach(() => {
  8.     actions = {
  9.       createUser: sinon.stub().resolves(true)
  10.     };
  11.  
  12.     const state = {
  13.       modules: {
  14.         users: {
  15.           namespaced: true,
  16.           actions
  17.         }
  18.       }
  19.     };
  20.  
  21.     store = new Vuex.Store(state);
  22.   });
  23.  
  24.   afterEach(() => {
  25.     actions.createUser.reset();
  26.   });
  27.  
  28.   describe('Methods', () => {
  29.     describe('Reset Fields', () => {
  30.       it('Should reset the fields to the initial data', done => {
  31.         const initialData = {
  32.           name: '',
  33.           email: '',
  34.           password: '',
  35.           avatar_url: ''
  36.         };
  37.  
  38.         const simulatedData = {
  39.           name: 'Cristiano Ronaldo',
  40.           email: 'cristiano@finta.pt',
  41.           password: 'cr7+eder',
  42.           avatar_url: 'https://reinaldo.pt/avatar.png'
  43.         };
  44.  
  45.         const wrapper = shallow(UserCreator,
  46.           {
  47.             data: simulatedData,
  48.             store
  49.           }
  50.         );
  51.  
  52.         expect(wrapper.data()).to.be.equal(simulatedData);
  53.  
  54.         wrapper.vm.resetFields();
  55.         expect(wrapper.data()).to.contain(initialData);
  56.         done();
  57.       });
  58.  
  59.       it('When reseting to the initial data, the dialog should not close', done => {
  60.         const initialData = {
  61.           name: '',
  62.           email: '',
  63.           password: '',
  64.           avatar_url: '',
  65.           dialog: true
  66.         };
  67.  
  68.         const simulatedData = {
  69.           name: 'Cristiano Ronaldo',
  70.           email: 'cristiano@finta.pt',
  71.           password: 'cr7+eder',
  72.           avatar_url: 'https://reinaldo.pt/avatar.png',
  73.           dialog: true
  74.         };
  75.  
  76.         const wrapper = shallow(UserCreator,
  77.           {
  78.             data: simulatedData,
  79.             store
  80.           }
  81.         );
  82.  
  83.         expect(wrapper.data()).to.be.equal(simulatedData);
  84.  
  85.         wrapper.vm.resetFields();
  86.         expect(wrapper.data()).to.deep.equal(initialData);
  87.         done();
  88.       });
  89.     });
  90.  
  91.     describe('CreateUser', () => {
  92.       it('Should do nothing if there is no email and password set', done => {
  93.         const wrapper = shallow(UserCreator,
  94.           { store }
  95.         );
  96.  
  97.         const result = wrapper.vm.createUser();
  98.         expect(result).to.be.false;
  99.  
  100.         // Check if the store wasn't called
  101.         expect(actions.createUser).to.not.have.been.called;
  102.  
  103.         done();
  104.       });
  105.  
  106.       it('Should do nothing if there is an email set, but no password', done => {
  107.         const email = 'teste@finta.com';
  108.         const wrapper = shallow(UserCreator,
  109.           {
  110.             data: {
  111.               email
  112.             },
  113.             store
  114.           }
  115.         );
  116.  
  117.         const result = wrapper.vm.createUser();
  118.         expect(result).to.be.false;
  119.         expect(wrapper.data().email).to.be.equal(email);
  120.  
  121.         // Check if the store wasn't called
  122.         expect(actions.createUser).to.not.have.been.called;
  123.  
  124.         done();
  125.       });
  126.  
  127.       it('Should do nothing if there is a password set, but no email', done => {
  128.         const password = 'asdasda';
  129.         const wrapper = shallow(UserCreator,
  130.           {
  131.             data: {
  132.               password
  133.             },
  134.             store
  135.           }
  136.         );
  137.  
  138.         const result = wrapper.vm.createUser();
  139.         expect(result).to.be.false;
  140.         expect(wrapper.data().password).to.be.equal(password);
  141.  
  142.         // Check if the store wasn't called
  143.         expect(actions.createUser).to.not.have.been.called;
  144.  
  145.         done();
  146.       });
  147.  
  148.       it('Should call the action if the email and password are set', done => {
  149.         const password = 'asdasdasda';
  150.         const email = 'teste@getfinta.com';
  151.         const wrapper = shallow(UserCreator,
  152.           {
  153.             data: {
  154.               password,
  155.               email
  156.             },
  157.             store
  158.           }
  159.         );
  160.  
  161.         expect(wrapper.data().password).to.be.equal(password);
  162.         expect(wrapper.data().email).to.be.equal(email);
  163.         const promise = wrapper.vm.createUser();
  164.  
  165.         promise.then(result => {
  166.           // Check if the store was called
  167.           expect(actions.createUser).to.have.been.called;
  168.           done();
  169.         }).catch(done);
  170.       });
  171.  
  172.       it('Should reset the fields changed and the dialog if the user was created', done => {
  173.         const password = 'asdasdasda';
  174.         const email = 'teste@getfinta.com';
  175.         const wrapper = shallow(UserCreator,
  176.           {
  177.             data: {
  178.               password,
  179.               email
  180.             },
  181.             store
  182.           }
  183.         );
  184.  
  185.         wrapper.data().dialog = true;
  186.  
  187.         expect(wrapper.data().password).to.be.equal(password);
  188.         expect(wrapper.data().email).to.be.equal(email);
  189.         const promise = wrapper.vm.createUser();
  190.  
  191.         // Create the spy
  192.         const spy = sinon.spy(wrapper.vm, 'resetFields');
  193.  
  194.         promise.then(result => {
  195.           // Check if the spy was called
  196.           expect(wrapper.data().dialog).to.be.equal(false);
  197.           expect(spy).to.have.been.called;
  198.           done();
  199.         }).catch(done);
  200.       });
  201.     });
  202.  
  203.  
  204.   });
  205. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement