donal
2018-04-13 0517f2f05b467f994970f29ebb1c851aba146ad9
ADD more fe tests cuz I love tests now :)
2 files modified
82 ■■■■ changed files
src/store/actions.js 10 ●●●● patch | view | raw | blame | history
tests/unit/javascript/actions.spec.js 72 ●●●● patch | view | raw | blame | history
src/store/actions.js
@@ -43,10 +43,10 @@
    // debugger
    const todo = {
      title: state.newTodo,
      completed: false,
      important: false
      completed: false
    };
    axios
    // console.info("TESTINT BLAH BLAH ", todo);
    return axios
      .post(config.todoEndpoint, todo)
      .then(mongoTodo => {
        commit("ADD_TODO", mongoTodo.data);
@@ -94,11 +94,11 @@
  updateTodo({ commit, state }, id) {
    let i = state.todos.findIndex(todo => todo._id === id);
    // todo - add back end
    axios
    return axios
      .put(config.todoEndpoint + "/" + state.todos[i]._id, state.todos[i])
      .then(data => {
        console.info("INFO - item " + id + " updated", data);
        commit("MARK_TODO_COMPLETED", i);
      });
    commit("MARK_TODO_COMPLETED", i);
  }
};
tests/unit/javascript/actions.spec.js
@@ -1,8 +1,8 @@
import actions from "@/store/actions";
import store from "@/store";
import * as all from "../setup.js";
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import sinon from "sinon";
const todos = [
@@ -10,12 +10,12 @@
  { id: 2, title: "learn testing 2", completed: false }
];
describe("load todos", () => {
  beforeEach(() =>{
describe("loadTodos", () => {
  beforeEach(() => {
    let mock = new MockAdapter(axios);
    mock.onGet('http://localhost:9000/api/todos').reply(200, todos);
    mock.onGet("http://localhost:9000/api/todos").reply(200, todos);
  });
  it("should call commit to the mutation function twice", (done) => {
  it("should call commit to the mutation function twice", done => {
    const commit = sinon.spy();
    actions.loadTodos({ commit }).then(() => {
      // console.log(commit)
@@ -23,7 +23,7 @@
      done();
    });
  });
  it("should first call SET_LOADING", (done) => {
  it("should first call SET_LOADING", done => {
    const commit = sinon.spy();
    actions.loadTodos({ commit }).then(() => {
      // console.log(commit.firstCall.args[0])
@@ -31,7 +31,7 @@
      done();
    });
  });
  it("should second call SET_TODOS", (done) => {
  it("should second call SET_TODOS", done => {
    const commit = sinon.spy();
    actions.loadTodos({ commit }).then(() => {
      // console.log(commit)
@@ -39,5 +39,61 @@
      done();
    });
  });
});
/*
  addTodo({ commit, state }) {
    if (!state.newTodo) {
      // do not add empty todos
      return;
    }
    // debugger
    const todo = {
      title: state.newTodo,
      completed: false,
      important: false
    };
    axios
      .post(config.todoEndpoint, todo)
      .then(mongoTodo => {
        commit("ADD_TODO", mongoTodo.data);
      })
      .catch(err => {
        if (err) {
          console.info("INFO - Adding dummy todo because of ", err);
          let mongoTodo = todo;
          mongoTodo._id = "fake-todo-item-" + Math.random();
          commit("ADD_TODO", mongoTodo);
        }
      });
  },
*/
let state;
describe("addTodos", () => {
  beforeEach(() => {
    state = {};
    let mock = new MockAdapter(axios);
    // mock.onPost(/http:\/\/localhost:9000\/api\/todos\/.*/, {})
    mock.onPost("http://localhost:9000/api/todos").reply(200, todos);
  });
  it("should call commit to the mutation function once", done => {
    const commit = sinon.spy();
    state.newTodo = "Learn some mocking";
    actions.addTodo({ commit, state }).then(() => {
      // console.log(commit)
      expect(commit.calledOnce).toBe(true);
      done();
    });
  });
  it("should first call ADD_TODO", done => {
    const commit = sinon.spy();
    state.newTodo = "Learn some mocking";
    actions.addTodo({ commit, state }).then(() => {
      // console.log(commit.firstCall.args[0])
      expect(commit.firstCall.args[0]).toBe("ADD_TODO");
      done();
    });
  });
});