@gov-cy/dsf-email-templates 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,179 @@
1
+ import { expect } from 'chai';
2
+ import { DSFEmailRenderer } from '../../src/index.mjs';
3
+
4
+
5
+ const renderer = new DSFEmailRenderer();
6
+
7
+ describe('1. Testing `DSFEmailRenderer.renderFromString`, use of `govcyBase.njk` and `macros`', () => {
8
+ const inputString = `
9
+ {% extends "govcyBase.njk" %}
10
+ {% from "govcyEmailElement.njk" import govcyEmailElement %}
11
+ {% set lang='el'%}
12
+ {% block lang %}{{lang}}{% endblock %}
13
+ {% block subject %}Service email{% endblock %}
14
+ {% block pre -%}{{ govcyEmailElement ('preHeader',{preText:'The pre header text'}) }}{%- endblock %}
15
+ {% block header -%}{{ govcyEmailElement ('header',{serviceName:'Service name', name:'First and Last name',lang:lang}) }}{%- endblock %}
16
+ {% block success -%}{{ govcyEmailElement ('success',{title:'title part', body:'body part'}) }}{%- endblock %}
17
+ {% block body -%}
18
+ {% call govcyEmailElement('body') -%}
19
+ {% call govcyEmailElement('bodyParagraph') -%}Paragraph{%- endcall %}
20
+ {% call govcyEmailElement('bodyHeading',{headinLevel:1}) -%}Heading 1{%- endcall %}
21
+ {% call govcyEmailElement('bodyHeading',{headinLevel:2}) -%}Heading 2{%- endcall %}
22
+ {% call govcyEmailElement('bodyHeading',{headinLevel:3}) -%}Heading 3{%- endcall %}
23
+ {% call govcyEmailElement('bodyHeading',{headinLevel:4}) -%}Heading 4{%- endcall %}
24
+ {% endcall %}
25
+ {% endblock %}
26
+ {% block footer -%}
27
+ {{ govcyEmailElement ('footer',{footerText:'Όνομα υπηρεσίας'}) }}
28
+ {%- endblock %}
29
+ `;
30
+ let renderedHTML = renderer.renderFromString(inputString);
31
+ //perform render checks
32
+ renderChecks(renderedHTML, '1.');
33
+ });
34
+
35
+ describe('2. Testing `DSFEmailRenderer.renderFromJson`', () => {
36
+ let inputJson={
37
+ lang: "el",
38
+ subject: "Service email",
39
+ pre: "The pre header text",
40
+ header: {serviceName:'Service name', name:'First and Last name'},
41
+ success: {
42
+ title:'title part',
43
+ body:'body part'
44
+ },
45
+ body: [
46
+ {"component": "bodyParagraph", body:"Paragraph"},
47
+ {"component": "bodyHeading",params: '{"headinLevel":1}',body:"Heading 1"},
48
+ {"component": "bodyHeading",params: '{"headinLevel":2}',body:"Heading 2"},
49
+ {"component": "bodyHeading",params: '{"headinLevel":3}',body:"Heading 3"},
50
+ {"component": "bodyHeading",params: '{"headinLevel":4}',body:"Heading 4"},
51
+ ],
52
+ footer: {
53
+ footerText: "Name of service"
54
+ }
55
+ }
56
+ let renderedHTML = renderer.renderFromJson(inputJson);
57
+ //perform render checks
58
+ renderChecks(renderedHTML, '2.');
59
+ });
60
+
61
+ /**
62
+ * Perform tests on rendered HTML
63
+ *
64
+ * @param {string} renderedHTML The rendered HTML
65
+ * @param {string} checksNum The prefix of the checksNum
66
+ */
67
+ function renderChecks(renderedHTML, checksNum){
68
+ it(checksNum+'1 `renderFromString` should render html doctype', async () => {
69
+ expect(renderedHTML).to.include('<!doctype html>');
70
+ });
71
+ it(checksNum+'2 `renderFromString` should render html tag', async () => {
72
+ expect(renderedHTML).to.include(`<html lang="el" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">`);
73
+ expect(renderedHTML).to.include(`</html>`);
74
+ });
75
+ it(checksNum+'3 `renderFromString` should render head section', async () => {
76
+ expect(renderedHTML).to.include(`<head>`);
77
+ expect(renderedHTML).to.include(`<title>Service email</title>`);
78
+ expect(renderedHTML).to.include(`<!--[if !mso]><!-->`);
79
+ expect(renderedHTML).to.include(`<meta http-equiv="X-UA-Compatible" content="IE=edge">`);
80
+ expect(renderedHTML).to.include(`<!--<![endif]-->`);
81
+ expect(renderedHTML).to.include(`<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">`);
82
+ expect(renderedHTML).to.include(`<meta name="viewport" content="width=device-width, initial-scale=1">`);
83
+ expect(renderedHTML).to.include(`</head>`);
84
+ });
85
+ it(checksNum+'4 `renderFromString` should render `<title>SUBJECT</title>`', async () => {
86
+ expect(renderedHTML).to.include(`<title>Service email</title>`);
87
+ });
88
+ it(checksNum+'5 `renderFromString` should render the body with the right table things and closing html', async () => {
89
+ expect(renderedHTML).to.include(`<body>`);
90
+ // Define the regular expression to match the expected segment
91
+ const expectedRegex = /<body>([\s\S]*?)<table border="0" cellpadding="0" cellspacing="0" width="100%">\s*<tr>\s*<td align="center">\s*<!--\[if mso\]>\s* <table align="center" border="0" cellspacing="0" cellpadding="0" width="600">\s*<tr>\s*<td align="center" valign="top" width="600">\s*<!\[endif\]-->([\s\S]*?)<!--\[if mso\]>\s*<\/td>\s*<\/tr>\s*<\/table>\s*<!\[endif\]-->\s*<\/td>\s*<\/tr>\s*<\/table>\s*<\/body>\s*<\/html>/;
92
+ // Check if the rendered HTML matches the regular expression pattern
93
+ expect(renderedHTML).to.match(expectedRegex);
94
+ expect(renderedHTML).to.include(`</body>`);
95
+ });
96
+ it(checksNum+'6 `pre` block and `preHeader` macro render as expected', async () => {
97
+ // Define the regular expression to match the expected segment
98
+ const expectedRegex = /<body>([\s\S]*?)<!-- PRE HEADER TEXT -->\s*<div style="display:none;font-size:1px;color:#ffffff;line-height:1px;max-height:0px;max-width:0px;opacity:0;overflow:hidden;">The pre header text<\/div>/;
99
+ // Check if the rendered HTML matches the regular expression pattern
100
+ expect(renderedHTML).to.match(expectedRegex);
101
+ });
102
+ it(checksNum+'7 `header` block and `header` macro render as expected', async () => {
103
+ // check for structure
104
+ let expectedRegex = /<body>([\s\S]*?)<!-- HEADER -->\s*<table([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<img([\s\S]*?)>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<div([\s\S]*?)>([\s\S]*?)<\/div>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<div([\s\S]*?)>([\s\S]*?)<\/div>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>([\s\S]*?)<\/table>/;
105
+ expect(renderedHTML).to.match(expectedRegex);
106
+ // table style
107
+ expectedRegex = /<body>([\s\S]*?)<!-- HEADER -->\s*<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;" >/;
108
+ expect(renderedHTML).to.match(expectedRegex);
109
+ // 1st td
110
+ expectedRegex = /<body>([\s\S]*?)<td align="left" valign="top" style="padding: 12px 16px 0px;background:#31576f;background-color:#31576f;">/;
111
+ expect(renderedHTML).to.match(expectedRegex);
112
+ // 2st td
113
+ expectedRegex = /<body>([\s\S]*?)<td align="left" valign="top" style="padding:0px 16px 12px;border-bottom: 4px solid #ffad2d;background:#31576f;background-color:#31576f;">/;
114
+ expect(renderedHTML).to.match(expectedRegex);
115
+ // service name div
116
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:Arial;font-size:18px;line-height:1.5;text-align:left; color:#ffffff;" >/;
117
+ expect(renderedHTML).to.match(expectedRegex);
118
+ // 3rd td
119
+ expectedRegex = /<body>([\s\S]*?)<td align="center" valign="top" style="padding:20px 16px;">/;
120
+ expect(renderedHTML).to.match(expectedRegex);
121
+ // salutation name div
122
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >/;
123
+ expect(renderedHTML).to.match(expectedRegex);
124
+ });
125
+ it(checksNum+'8 `success` block and `success` macro render as expected', async () => {
126
+ //check for structure
127
+ let expectedRegex = /<body>([\s\S]*?)<!-- SUCCESS -->\s*<table([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<div([\s\S]*?)>([\s\S]*?)<\/div>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<div([\s\S]*?)>([\s\S]*?)<\/div>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>([\s\S]*?)<\/table>/;
128
+ expect(renderedHTML).to.match(expectedRegex);
129
+ // table style
130
+ expectedRegex = /<body>([\s\S]*?)<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding:10px 0;max-width: 600px;background:#00703c;background-color:#00703c;" >/;
131
+ expect(renderedHTML).to.match(expectedRegex);
132
+ //success title style
133
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:helvetica;font-size:24px;font-weight:bold;line-height:1.5;text-align:center;color:#ffffff;">([\s\S]*?)<\/div>/;
134
+ expect(renderedHTML).to.match(expectedRegex);
135
+ //success body style
136
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:Arial;font-size:18px;line-height:1.5;text-align:center;color:#ffffff;">([\s\S]*?)<\/div>/;
137
+ expect(renderedHTML).to.match(expectedRegex);
138
+ });
139
+ it(checksNum+'9 `bodyParagraph` macro render as expected', async () => {
140
+ // Define the regular expression to match the expected segment
141
+ const expectedRegex = /<body>([\s\S]*?)<tr>\s*<td align="center" valign="top" style="padding:10px 16px;">\s*<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >\s*Paragraph\s*<\/div>\s*<\/td>\s*<\/tr>/;
142
+ // Check if the rendered HTML matches the regular expression pattern
143
+ expect(renderedHTML).to.match(expectedRegex);
144
+ });
145
+ it(checksNum+'10 `bodyHeading` macro render as expected', async () => {
146
+ // Heading 1
147
+ let expectedRegex = /<body>([\s\S]*?)<tr>\s*<td align="center" valign="top" style="padding:10px 16px;">\s*<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >\s*<h1 style="font-size:28px;font-weight:700;margin: 0;">\s*Heading 1\s*<\/h1>\s*<\/div>\s*<\/td>\s*<\/tr>/;
148
+ expect(renderedHTML).to.match(expectedRegex);
149
+ // Heading 2
150
+ expectedRegex = /<body>([\s\S]*?)<tr>\s*<td align="center" valign="top" style="padding:10px 16px;">\s*<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >\s*<h2 style="font-size:24px;font-weight:700;margin: 0;">\s*Heading 2\s*<\/h2>\s*<\/div>\s*<\/td>\s*<\/tr>/;
151
+ expect(renderedHTML).to.match(expectedRegex);
152
+ // Heading 3
153
+ expectedRegex = /<body>([\s\S]*?)<tr>\s*<td align="center" valign="top" style="padding:10px 16px;">\s*<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >\s*<h3 style="font-size:20px;font-weight:700;margin: 0;">\s*Heading 3\s*<\/h3>\s*<\/div>\s*<\/td>\s*<\/tr>/;
154
+ expect(renderedHTML).to.match(expectedRegex);
155
+ // Heading 4
156
+ expectedRegex = /<body>([\s\S]*?)<tr>\s*<td align="center" valign="top" style="padding:10px 16px;">\s*<div style="font-family:Arial;font-size:16px;line-height:1.5;text-align:left;" >\s*<h4 style="font-size:18px;font-weight:700;margin: 0;">\s*Heading 4\s*<\/h4>\s*<\/div>\s*<\/td>\s*<\/tr>/;
157
+ expect(renderedHTML).to.match(expectedRegex);
158
+ });
159
+ it(checksNum+'11 `footer` block and `footer` macro render as expected', async () => {
160
+ //check for structure
161
+ let expectedRegex = /<body>([\s\S]*?)<table([\s\S]*?)>\s*<tr([\s\S]*?)>\s*<td([\s\S]*?)>\s*<div([\s\S]*?)>\s*<a([\s\S]*?)>\s*<strong([\s\S]*?)>([\s\S]*?)<\/strong>\s*<\/a>\s*<\/div>\s*<div([\s\S]*?)>([\s\S]*?)<\/div>\s*<\/td([\s\S]*?)>\s*<\/tr([\s\S]*?)>\s*<\/table>/;
162
+ expect(renderedHTML).to.match(expectedRegex);
163
+ // td style
164
+ expectedRegex = /<body>([\s\S]*?)<td align="center" valign="top" style="padding:10px 16px 10px;border-top: 4px solid #ffad2d;background:#ebf1f3;background-color:#ebf1f3;">/;
165
+ expect(renderedHTML).to.match(expectedRegex);
166
+ // govcy div style
167
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:Arial; font-size: 24px;text-align:left;margin-bottom: 10px;line-height:1;">/;
168
+ expect(renderedHTML).to.match(expectedRegex);
169
+ // govcy a style
170
+ expectedRegex = /<body>([\s\S]*?)<a href="https:\/\/gov.cy" style="color:#000000; text-decoration: none;font-weight: 500;">/;
171
+ expect(renderedHTML).to.match(expectedRegex);
172
+ // govcy strong style
173
+ expectedRegex = /<body>([\s\S]*?)<strong style="color:#000000; text-decoration: none;font-weight: 500;">/;
174
+ expect(renderedHTML).to.match(expectedRegex);
175
+ // footer second line style
176
+ expectedRegex = /<body>([\s\S]*?)<div style="font-family:Arial;font-size:16px;text-align:left;margin-bottom: 10px;line-height:1;" >/;
177
+ expect(renderedHTML).to.match(expectedRegex);
178
+ });
179
+ }