summaryrefslogtreecommitdiff
path: root/spec/frontend/static_site_editor/services/templater_spec.js
blob: 1e7ae872b7e466864d8371123f5132f6b7e9494c (plain)
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
/* eslint-disable no-useless-escape */
import templater from '~/static_site_editor/services/templater';

describe('templater', () => {
  const source = `Below this line is a simple ERB (single-line erb block) example.

<% some erb code %>

Below this line is a complex ERB (multi-line erb block) example.

<% if apptype.maturity && (apptype.maturity != "planned") %>
  <% maturity = "This application type is at the \"#{apptype.maturity}\" level of maturity." %>
<% end %>

Below this line is a non-erb (single-line HTML) markup example that also has erb.

<a href="<%= compensation_roadmap.role_path %>"><%= compensation_roadmap.role_path %></a>

Below this line is a non-erb (multi-line HTML block) markup example that also has erb.

<ul>
<% compensation_roadmap.recommendation.recommendations.each do |recommendation| %>
  <li><%= recommendation %></li>
<% end %>
</ul>

Below this line is a block of HTML.

<div>
  <h1>Heading</h1>
  <p>Some paragraph...</p>
</div>

Below this line is a codeblock of the same HTML that should be ignored and preserved.

\`\`\` html
<div>
  <h1>Heading</h1>
  <p>Some paragraph...</p>
</div>
\`\`\`
`;
  const sourceTemplated = `Below this line is a simple ERB (single-line erb block) example.

\`\`\` sse
<% some erb code %>
\`\`\`

Below this line is a complex ERB (multi-line erb block) example.

\`\`\` sse
<% if apptype.maturity && (apptype.maturity != "planned") %>
  <% maturity = "This application type is at the \"#{apptype.maturity}\" level of maturity." %>
<% end %>
\`\`\`

Below this line is a non-erb (single-line HTML) markup example that also has erb.

\`\`\` sse
<a href="<%= compensation_roadmap.role_path %>"><%= compensation_roadmap.role_path %></a>
\`\`\`

Below this line is a non-erb (multi-line HTML block) markup example that also has erb.

\`\`\` sse
<ul>
<% compensation_roadmap.recommendation.recommendations.each do |recommendation| %>
  <li><%= recommendation %></li>
<% end %>
</ul>
\`\`\`

Below this line is a block of HTML.

\`\`\` sse
<div>
  <h1>Heading</h1>
  <p>Some paragraph...</p>
</div>
\`\`\`

Below this line is a codeblock of the same HTML that should be ignored and preserved.

\`\`\` html
<div>
  <h1>Heading</h1>
  <p>Some paragraph...</p>
</div>
\`\`\`
`;

  it.each`
    fn          | initial            | target
    ${'wrap'}   | ${source}          | ${sourceTemplated}
    ${'wrap'}   | ${sourceTemplated} | ${sourceTemplated}
    ${'unwrap'} | ${sourceTemplated} | ${source}
    ${'unwrap'} | ${source}          | ${source}
  `(
    'wraps $initial in a templated sse codeblocks if $fn is wrap, unwraps otherwise',
    ({ fn, initial, target }) => {
      expect(templater[fn](initial)).toMatch(target);
    },
  );
});