-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_case.js
169 lines (134 loc) · 3.97 KB
/
test_case.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ### Test Case 1: Simple Template Rendering
// **Description**: Test with a simple template and context data.
const sendEmail = require("./send_email")
const cleanString = str => str.replace(/\s/g, '');
const assert = require('assert');
const templateCode = `
<html>
<body>
<h1>Hello, {{first_name}} {{last_name}}!</h1>
<p>Thank you for registering with {{company}}.</p>
</body>
</html>
`;
const contextData = {
first_name: 'John',
last_name: 'Doe',
company: 'Techdome'
};
const expectedOutput = `
<html>
<body>
<h1>Hello, John Doe!</h1>
<p>Thank you for registering with Techdome.</p>
</body>
</html>
`;
const result = sendEmail(null, '[email protected]', contextData, templateCode, []);
assert.strictEqual(cleanString(result.renderedHtml), cleanString(expectedOutput), 'Test Case 1 Failed');
console.log('Test Case 1 Passed');
// ### Test Case 2: Missing Context Variable
// **Description**: Test how the function handles a missing context variable:
const templateCode2 = `
<html>
<body>
<h1>Hello, {{first_name}} {{last_name}}!</h1>
<p>Thank you for registering with {{company}}.</p>
</body>
</html>
`;
const contextData2 = {
first_name: 'John',
company: 'Techdome'
};
const expectedOutput2 = `
<html>
<body>
<h1>Hello, John {{last_name}}!</h1>
<p>Thank you for registering with Techdome.</p>
</body>
</html>
`;
const result2 = sendEmail(null, '[email protected]', contextData2, templateCode2, []);
assert.strictEqual(cleanString(result2.renderedHtml), cleanString(expectedOutput2), 'Test Case 2 Failed');
console.log('Test Case 2 Passed');
// ### Test Case 3: Complex Template with Conditionals
// **Description**: Test with an advanced template that includes conditionals.
const templateCode3 = `
<html>
<body>
{% if first_name %}
<h1>Hello, {{first_name}}!</h1>
{% else %}
<h1>Hello, Guest!</h1>
{% endif %}
<p>Thank you for registering.</p>
</body>
</html>
`;
const contextData3 = {
first_name: 'Jane'
};
const expectedOutput3 = `
<html>
<body>
<h1>Hello, Jane!</h1>
<p>Thank you for registering.</p>
</body>
</html>
`;
const result3 = sendEmail(null, '[email protected]', contextData3, templateCode3, []);
assert.strictEqual(cleanString(result3.renderedHtml), cleanString(expectedOutput3), 'Test Case 3 Failed');
console.log('Test Case 3 Passed');
// ### Test Case 4: Email Validation
// **Description**: Test with an invalid email address.
try {
sendEmail(null, 'invalid-email', {}, '', []);
assert.fail('Test Case 4 Failed: No error thrown for invalid email');
} catch (err) {
assert.strictEqual(err.message, 'Invalid email address', 'Test Case 4 Failed');
console.log('Test Case 4 Passed');
}
// ### Test Case 5: Attachment Validation
// **Description**: Test with an invalid attachment path.
const templateCode4 = '<html><body>Test</body></html>';
const contextData4 = {};
try {
sendEmail(null, '[email protected]', contextData4, templateCode4, ['/invalid/path.jpg']);
assert.fail('Test Case 5 Failed: No error thrown for invalid attachment path');
} catch (err) {
assert.strictEqual(err.message, 'Attachment file not found: /invalid/path.jpg', 'Test Case 5 Failed');
console.log('Test Case 5 Passed');
}
// ### Test Case 6: Advanced Templating with Loop
// **Description**: Test with a template that includes a loop to list items.
const templateCode5 = `
<html>
<body>
<h1>Shopping List:</h1>
<ul>
{% for item in items %}
<li>{{item}}</li>
{% endfor %}
</ul>
</body>
</html>
`;
const contextData5 = {
items: ['Milk', 'Bread', 'Eggs']
};
const expectedOutput5 = `
<html>
<body>
<h1>Shopping List:</h1>
<ul>
<li>Milk</li>
<li>Bread</li>
<li>Eggs</li>
</ul>
</body>
</html>
`;
const result5 = sendEmail(null, '[email protected]', contextData5, templateCode5, []);
assert.strictEqual(cleanString(result5.renderedHtml), cleanString(expectedOutput5), 'Test Case 6 Failed');
console.log('Test Case 6 Passed');