Passed
Push — master ( 6fca9e...82ba0d )
by Huu-Phat
02:25 queued 10s
created

cms/src/post/state/__tests__/reducers.spec.js   A

Complexity

Total Complexity 29
Complexity/F 1

Size

Lines of Code 189
Function Count 29

Duplication

Duplicated Lines 42
Ratio 22.22 %

Importance

Changes 0
Metric Value
wmc 29
eloc 128
mnd 0
bc 0
fnc 29
dl 42
loc 189
bpm 0
cpm 1
noi 1
c 0
b 0
f 0
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import postReducer from '../reducers'
2
import {
3
  FETCH_POST,
4
  EDIT_POST_TITLE,
5
  EDIT_POST_SLUG,
6
  EDIT_POST_BRIEF,
7
  EDIT_POST_AUTHOR,
8
  EDIT_POST_TIME,
9
  EDIT_POST_TAGS,
10
  EDIT_POST_CONTENT,
11
  SAVE_EDITED_CONTENT,
12
  CREATE_NEW_POST,
13
  INIT_POST
14
} from 'core/state/actionType'
15
import { toRequest, toSuccess, toError } from 'core/state/utils'
16
17
describe('Post Reducer', () => {
18
  describe('default', () => {
19
    it('should work corectly', function() {
20
      const action = {
21
        type: 'default'
22
      }
23
      expect(postReducer({}, action)).toMatchSnapshot()
24
      expect(() => {
25
        postReducer()
26
      }).toThrow(TypeError)
27
    })
28
  })
29
30
  describe('FETCH_POST', () => {
31
    it('toRequest should work corectly', function() {
32
      const action = {
33
        type: toRequest(FETCH_POST)
34
      }
35
      expect(postReducer({}, action)).toMatchSnapshot()
36
    })
37
38
    it('toSuccess should work corectly', function() {
39
      const action = {
40
        type: toSuccess(FETCH_POST),
41
        payload: {
42
          id: 'test',
43
          slug: 'test',
44
          title: 'test',
45
          brief: 'test',
46
          time: 'test',
47
          tag: ['test', 'test1'],
48
          author: 'test',
49
          content: 'test'
50
        }
51
      }
52
      expect(postReducer({}, action)).toMatchSnapshot()
53
    })
54
55
    it('toError should work corectly', function() {
56
      const action = {
57
        type: toError(FETCH_POST),
58
        error: new Error('test')
59
      }
60
      expect(() => {
61
        postReducer({}, action)
62
      }).toThrow(Error)
63
    })
64
  })
65
66 View Code Duplication
  describe('CREATE_NEW_POST', () => {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    it('toSuccess should work corectly', function() {
68
      const state = {
69
        editedContent: {
70
          id: 'test',
71
          slug: 'test',
72
          title: 'test',
73
          brief: 'test',
74
          time: 'test',
75
          tag: ['test', 'test1'],
76
          author: 'test',
77
          content: 'test'
78
        }
79
      }
80
      const action = {
81
        type: toSuccess(CREATE_NEW_POST),
82
        payload: {}
83
      }
84
      expect(postReducer(state, action)).toMatchSnapshot()
85
    })
86
  })
87
88 View Code Duplication
  describe('SAVE_EDITED_CONTENT', () => {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
89
    it('toSuccess should work corectly', function() {
90
      const state = {
91
        editedContent: {
92
          id: 'test',
93
          slug: 'test',
94
          title: 'test',
95
          brief: 'test',
96
          time: 'test',
97
          tag: ['test', 'test1'],
98
          author: 'test',
99
          content: 'test'
100
        }
101
      }
102
      const action = {
103
        type: toSuccess(SAVE_EDITED_CONTENT),
104
        payload: {}
105
      }
106
      expect(postReducer(state, action)).toMatchSnapshot()
107
    })
108
  })
109
110
  describe('EDIT_POST_TITLE', () => {
111
    it('should work corectly', function() {
112
      const action = {
113
        type: EDIT_POST_TITLE,
114
        payload: 'edited title'
115
      }
116
      expect(postReducer({}, action)).toMatchSnapshot()
117
    })
118
  })
119
120
  describe('EDIT_POST_SLUG', () => {
121
    it('should work corectly', function() {
122
      const action = {
123
        type: EDIT_POST_SLUG,
124
        payload: 'edited-slug'
125
      }
126
      expect(postReducer({}, action)).toMatchSnapshot()
127
    })
128
  })
129
130
  describe('EDIT_POST_BRIEF', () => {
131
    it('should work corectly', function() {
132
      const action = {
133
        type: EDIT_POST_BRIEF,
134
        payload: 'edited brief'
135
      }
136
      expect(postReducer({}, action)).toMatchSnapshot()
137
    })
138
  })
139
140
  describe('EDIT_POST_AUTHOR', () => {
141
    it('should work corectly', function() {
142
      const action = {
143
        type: EDIT_POST_AUTHOR,
144
        payload: 'edited author'
145
      }
146
      expect(postReducer({}, action)).toMatchSnapshot()
147
    })
148
  })
149
150
  describe('EDIT_POST_TIME', () => {
151
    it('should work corectly', function() {
152
      const action = {
153
        type: EDIT_POST_TIME,
154
        payload: new Date('04/04/2020')
155
      }
156
      expect(postReducer({}, action)).toMatchSnapshot()
157
    })
158
  })
159
160
  describe('EDIT_POST_TAGS', () => {
161
    it('should work corectly', function() {
162
      const action = {
163
        type: EDIT_POST_TAGS,
164
        payload: 'edited tag'
165
      }
166
      expect(postReducer({}, action)).toMatchSnapshot()
167
    })
168
  })
169
170
  describe('EDIT_POST_CONTENT', () => {
171
    it('should work corectly', function() {
172
      const action = {
173
        type: EDIT_POST_CONTENT,
174
        payload: 'edited content'
175
      }
176
      expect(postReducer({}, action)).toMatchSnapshot()
177
    })
178
  })
179
180
  describe('INIT_POST', () => {
181
    it('should work corectly', function() {
182
      jest.spyOn(Date, 'now').mockReturnValue(new Date(1586001600000))
1 ignored issue
show
Bug introduced by
The variable jest seems to be never declared. If this is a global, consider adding a /** global: jest */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
183
      const action = {
184
        type: INIT_POST
185
      }
186
      expect(postReducer({}, action)).toMatchSnapshot()
187
    })
188
  })
189
})
190