action.spec.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Created by zhengguorong on 2016/11/30.
  3. */
  4. import Article from 'src/model/Article'
  5. import tools from '../../tools'
  6. const actionsInjector = require('inject!src/vuex/article/actions')
  7. const article = new Article()
  8. var articleList = []
  9. articleList.push(article)
  10. console.log(actionsInjector)
  11. // actionsInjector的作用是替代aja,使用模拟数据替代请求接口,../../api/article为请求ajax的方法,getArticleList覆盖该文件的对应方法。
  12. const actions = actionsInjector({
  13. // 文件路径相对于当前action
  14. '../../api/article': {
  15. getArticleList () {
  16. return new Promise((resolve, reject) => {
  17. setTimeout(() => {
  18. resolve(articleList)
  19. }, 100)
  20. })
  21. },
  22. createArticle (article, cb) {
  23. return new Promise((resolve, reject) => {
  24. setTimeout(() => {
  25. resolve(article)
  26. }, 100)
  27. })
  28. },
  29. updateArticle (articleForUpdate, cb) {
  30. return new Promise((resolve, reject) => {
  31. setTimeout(() => {
  32. resolve(articleForUpdate)
  33. }, 100)
  34. })
  35. }
  36. }
  37. })
  38. describe('article actions', () => {
  39. it('获取文章列表不为空', done => {
  40. tools.testAction(actions.getArticleList, [], {}, [
  41. { type: 'GET_ARTICLE_LIST', payload: articleList },
  42. { type: 'SET_EDITOR_ARTICLE', payload: articleList[0] }
  43. ], done)
  44. })
  45. it('获取文章列表为空', done => {
  46. articleList = []
  47. tools.testAction(actions.getArticleList, [], {}, [
  48. { type: 'SET_EDITOR_ARTICLE', payload: article }
  49. ], done)
  50. })
  51. it('添加文章', done => {
  52. tools.testAction(actions.addArticle, [], {}, [
  53. { type: 'ADD_ARTICLE', payload: article }
  54. ], done)
  55. })
  56. it('设置正在编辑的文章', done => {
  57. tools.testAction(actions.setEditorArticle, [], {}, [
  58. { type: 'SET_EDITOR_ARTICLE', payload: article }
  59. ], done)
  60. })
  61. it('保存文章', done => {
  62. tools.testAction(actions.saveArticle, [], {}, [
  63. { type: 'CREATE_ARTICLE_SUCCESS', payload: '' }
  64. ], done)
  65. })
  66. it('更新文章', done => {
  67. var updateArticle = article
  68. updateArticle._id = '1234567'
  69. tools.testAction(actions.saveArticle, [updateArticle], {}, [
  70. { type: 'UPDATE_ARTICLE_SUCCESS', payload: '' }
  71. ], done)
  72. })
  73. })