Conditions | 1 |
Paths | 1 |
Total Lines | 88 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import chai, { expect } from 'chai' |
||
16 | describe('/POST email', () => { |
||
17 | const toAddress = '[email protected]' |
||
18 | const ccAddress = '[email protected]' |
||
19 | const bccAddress = '[email protected]' |
||
20 | const replyToAddress = '[email protected]' |
||
21 | const htmlEmail = '<p>HTML Email</p>' |
||
22 | const textEmail = 'Text Email' |
||
23 | const emailSubject = 'Email Subject 😊' |
||
24 | const fromEmail = '[email protected]' |
||
25 | it('should succeed if email has all params', (done) => { |
||
26 | chai.request(server) |
||
27 | .post('/') |
||
28 | .send({ |
||
29 | Action: 'SendEmail', |
||
30 | 'Destination.ToAddresses.member.1': toAddress, |
||
31 | 'Message.Body.Html.Data': htmlEmail, |
||
32 | 'Message.Body.Text.Data': textEmail, |
||
33 | 'Message.Subject.Data': emailSubject, |
||
34 | 'Destination.CcAddresses.member.1': ccAddress, |
||
35 | 'Destination.BccAddresses.member.1': bccAddress, |
||
36 | 'ReplyToAddresses.member.1': replyToAddress, |
||
37 | Source: fromEmail, |
||
38 | }) |
||
39 | .end((err, res) => { |
||
40 | expect(res).to.have.status(200) |
||
41 | const response = new xmldoc.XmlDocument(res.text) |
||
42 | const path = response.valueWithPath('SendEmailResult.MessageId') |
||
43 | expect(fs.readFileSync(path, 'utf8')).to.eq(htmlEmail) |
||
44 | expect(fs.readFileSync(path.replace('body.html', 'body.txt'), 'utf8')).to.eq(textEmail) |
||
45 | expect(fs.readFileSync(path.replace('body.html', 'headers.txt'), 'utf8')).to.eq(`Subject: ${emailSubject}\nTo Address: ${toAddress}\nCc Address: ${ccAddress}\nBcc Address: ${bccAddress}\nReply To Address: ${replyToAddress}\nSource: ${fromEmail}`) |
||
46 | done() |
||
47 | }) |
||
48 | }) |
||
49 | it('should succeed if only HTML body is missing', (done) => { |
||
50 | chai.request(server) |
||
51 | .post('/') |
||
52 | .send({ |
||
53 | Action: 'SendEmail', |
||
54 | 'Destination.ToAddresses.member.1': toAddress, |
||
55 | 'Message.Body.Text.Data': textEmail, |
||
56 | 'Message.Subject.Data': emailSubject, |
||
57 | 'Destination.CcAddresses.member.1': ccAddress, |
||
58 | 'Destination.BccAddresses.member.1': bccAddress, |
||
59 | 'ReplyToAddresses.member.1': replyToAddress, |
||
60 | Source: fromEmail, |
||
61 | }) |
||
62 | .end((err, res) => { |
||
63 | expect(res).to.have.status(200) |
||
64 | done() |
||
65 | }) |
||
66 | }) |
||
67 | it('should succeed if only Text body is missing', (done) => { |
||
68 | chai.request(server) |
||
69 | .post('/') |
||
70 | .send({ |
||
71 | Action: 'SendEmail', |
||
72 | 'Destination.ToAddresses.member.1': toAddress, |
||
73 | 'Message.Body.Html.Data': htmlEmail, |
||
74 | 'Message.Subject.Data': emailSubject, |
||
75 | 'Destination.CcAddresses.member.1': ccAddress, |
||
76 | 'Destination.BccAddresses.member.1': bccAddress, |
||
77 | 'ReplyToAddresses.member.1': replyToAddress, |
||
78 | Source: fromEmail, |
||
79 | }) |
||
80 | .end((err, res) => { |
||
81 | expect(res).to.have.status(200) |
||
82 | done() |
||
83 | }) |
||
84 | }) |
||
85 | it('should fail if one param is not sent', (done) => { |
||
86 | chai.request(server) |
||
87 | .post('/') |
||
88 | .send({ |
||
89 | Action: 'SendEmail', |
||
90 | 'Message.Body.Html.Data': htmlEmail, |
||
91 | 'Message.Body.Text.Data': textEmail, |
||
92 | 'Message.Subject.Data': emailSubject, |
||
93 | Source: fromEmail, |
||
94 | }) |
||
95 | .end((err, res) => { |
||
96 | expect(res).to.have.status(500) |
||
97 | const response = new xmldoc.XmlDocument(res.text) |
||
98 | expect(response.valueWithPath('Code')).to.eq('MessageRejected') |
||
99 | expect(response.valueWithPath('Message')).to.eq('One or more required fields was not sent') |
||
100 | done() |
||
101 | }) |
||
102 | }) |
||
103 | }) |