UNPKG

4.44 kBJavaScriptView Raw
1var should = require("should");
2
3var fs = require("fs");
4var path = require("path");
5
6var runLoader = require("./fakeModuleSystem");
7var twigLoader = require("../");
8
9var fixtures = path.join(__dirname, "fixtures");
10
11describe("include", function () {
12 it("should generate correct code", function (done) {
13 var template = path.join(fixtures, "include", "template.html.twig");
14 runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
15 if (err) throw err;
16
17 result.should.have.type("string");
18
19 // verify the generated module imports the `include`d templates
20 result.should.match(/require\(\"\.\/a\.html\.twig\"\);/);
21 result.should.match(/require\(\"\.\/b\.html\"\);/); // test webpack extension resolve
22 result.should.match(/require\(\"\.\/c\.html\.twig\"\);/);
23 result.should.match(/require\(\"\.\/d\.html\.twig\"\);/);
24 result.should.match(/require\(\"\.\/e\.html\.twig\"\);/);
25 result.should.match(/require\(\"\.\/f\.html\.twig\"\);/);
26 result.should.match(/require\(\"\.\/g\.html\.twig\"\);/);
27
28 done();
29 });
30 });
31
32 // dynamic includes can never be resolved by webpack,
33 // so they are probably registered at runtime by the end user
34 it("should leave dynamic includes in tact", function (done) {
35 var template = path.join(fixtures, "include", "template.dynamic.html.twig");
36 runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
37 if (err) throw err;
38
39 result.should.have.type("string");
40
41 // verify the dynamic modules don't end up as require statements
42 result.should.not.match(/require\("~"\);/);
43 result.should.not.match(/require\(".\/"\);/);
44 result.should.not.match(/require\("name"\);/);
45 result.should.not.match(/require\("block\.name"\);/);
46 // it might be better to test the actual result tokens, but since the output is a string,
47 // it's tricky to do those matches.
48
49 done();
50 });
51 });
52
53 // testing for static includes that cannot be resolved by webpack,
54 // so they are probably registered at runtime by the end user
55 it("should leave non-existing includes in tact", function (done) {
56 var template = path.join(fixtures, "include", "template.alias.html.twig");
57 runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
58 if (err) throw err;
59
60 result.should.have.type("string");
61
62 // verify the dynamic modules don't end up as require statements
63 result.should.not.match(/require\("foo"\);/);
64 // it might be better to test the actual result tokens, but since the output is a string,
65 // it's tricky to do those matches.
66
67 done();
68 });
69 });
70
71 // testing to see
72 it("should generate same template id for resource and dependency", function (done) {
73 var template = path.join(fixtures, "include", "template.nested.html.twig");
74 runLoader(twigLoader, path.join(fixtures, "include"), template, fs.readFileSync(template, "utf-8"), function (err, result) {
75 if (err) throw err;
76
77 result.should.have.type("string");
78
79 result.should.match(/require\("\.\/nested\.html"\);/);
80
81 // the template id that is in the 'include' to reference 'nested.html.twig'
82 var nestedTemplateId = result.match(/"value":"([^"]+)"/i)[1];
83
84 // check template id of nested template
85 var nestedTemplate = path.join(fixtures, "include", "nested.html.twig");
86 runLoader(twigLoader, path.join(fixtures, "include"), nestedTemplate, fs.readFileSync(nestedTemplate, "utf-8"), function (err, result) {
87 if (err) throw err;
88
89 result.should.have.type("string");
90
91 // the ID for the template 'nested.html.twig', this should match the one in the parent template
92 // that references this template
93 var templateId = result.match(/twig\({"id":"([^"]+)"/i)[1];
94
95 templateId.should.equal(nestedTemplateId);
96
97 done();
98 });
99 });
100 });
101});