Skip to content

Commit

Permalink
Refactor to get rid of _test method caused by changes to #915
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Sep 27, 2021
1 parent c1b9ebe commit 508114a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
12 changes: 0 additions & 12 deletions src/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,18 +378,6 @@ class TemplateData {
return this.getData();
}

// TODO get rid of this, it’s been refactored to separate `getTemplateDirectoryData` and `getGlobalData` calls
async _testGetLocalData(templatePath) {
let localDataPaths = await this.getLocalDataPaths(templatePath);
let importedData = await this.combineLocalData(localDataPaths);
let globalData = await this.getData();

// OK-ish: shallow merge when combining template/data dir files with global data files
let localData = Object.assign({}, globalData, importedData);
// debug("`getLocalData` for %o: %O", templatePath, localData);
return localData;
}

getUserDataExtensions() {
if (!this.config.dataExtensions) {
return [];
Expand Down
23 changes: 19 additions & 4 deletions test/TemplateDataTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ const test = require("ava");
const TemplateData = require("../src/TemplateData");
const TemplateConfig = require("../src/TemplateConfig");

async function testGetLocalData(tmplData, templatePath) {
let localDataPaths = await tmplData.getLocalDataPaths(templatePath);
let importedData = await tmplData.combineLocalData(localDataPaths);
let globalData = await tmplData.getData();

// OK-ish: shallow merge when combining template/data dir files with global data files
let localData = Object.assign({}, globalData, importedData);
// debug("`getLocalData` for %o: %O", templatePath, localData);
return localData;
}

test("Create", async (t) => {
let eleventyConfig = new TemplateConfig();
let config = eleventyConfig.getConfig();
Expand Down Expand Up @@ -61,7 +72,8 @@ test("Add local data", async (t) => {
t.is(data.globalData.datakey1, "datavalue1");
t.is(data.globalData.datakey2, "@11ty/eleventy");

let withLocalData = await dataObj._testGetLocalData(
let withLocalData = await testGetLocalData(
dataObj,
"./test/stubs/component/component.njk"
);
t.is(withLocalData.globalData.datakey1, "datavalue1");
Expand All @@ -79,7 +91,8 @@ test("Get local data async JS", async (t) => {
let eleventyConfig = new TemplateConfig();
let dataObj = new TemplateData("./test/stubs/", eleventyConfig);

let withLocalData = await dataObj._testGetLocalData(
let withLocalData = await testGetLocalData(
dataObj,
"./test/stubs/component-async/component.njk"
);

Expand All @@ -97,7 +110,8 @@ test("addLocalData() doesn’t exist but doesn’t fail (template file does exis
let beforeDataKeyCount = Object.keys(data);

// template file does exist
let withLocalData = await dataObj._testGetLocalData(
let withLocalData = await testGetLocalData(
dataObj,
"./test/stubs/datafiledoesnotexist/template.njk"
);
t.is(withLocalData.globalData.datakey1, "datavalue1");
Expand All @@ -113,7 +127,8 @@ test("addLocalData() doesn’t exist but doesn’t fail (template file does not
let data = await dataObj.getData();
let beforeDataKeyCount = Object.keys(data);

let withLocalData = await dataObj._testGetLocalData(
let withLocalData = await testGetLocalData(
dataObj,
"./test/stubs/datafiledoesnotexist/templatedoesnotexist.njk"
);
t.is(withLocalData.globalData.datakey1, "datavalue1");
Expand Down
12 changes: 8 additions & 4 deletions test/TemplateTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,11 @@ test("Posts inherits local JSON, layouts", async (t) => {
"./test/stubs/posts/post1.11tydata.js",
]);

let localData = await dataObj._testGetLocalData(tmpl.getInputPath());
let localData = await dataObj.getTemplateDirectoryData(tmpl.getInputPath());
t.is(localData.layout, "mylocallayout.njk");
t.truthy(localData.pkg);

let globalData = await dataObj.getGlobalData();
t.truthy(globalData.pkg);

let data = await tmpl.getData();
t.is(localData.layout, "mylocallayout.njk");
Expand Down Expand Up @@ -565,9 +567,11 @@ test("Template and folder name are the same, make sure data imports work ok", as
"./test/stubs/posts/posts.11tydata.js",
]);

let localData = await dataObj._testGetLocalData(tmpl.getInputPath());
let localData = await dataObj.getTemplateDirectoryData(tmpl.getInputPath());
t.is(localData.layout, "mylocallayout.njk");
t.truthy(localData.pkg);

let globalData = await dataObj.getGlobalData();
t.truthy(globalData.pkg);

let data = await tmpl.getData();
t.is(localData.layout, "mylocallayout.njk");
Expand Down
2 changes: 1 addition & 1 deletion test/UserDataExtensionsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test("Local data", async (t) => {
t.is(data.globalData4.datakey1, "datavalue4");
t.is(data.globalData4.datakey2, "@11ty/eleventy--nosj");

let withLocalData = await dataObj._testGetLocalData(
let withLocalData = await dataObj.getTemplateDirectoryData(
"./test/stubs-630/component-yaml/component.njk"
);

Expand Down

0 comments on commit 508114a

Please sign in to comment.