Skip to content

Commit

Permalink
fix(index): resolves #6 for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Lynam committed Apr 23, 2020
1 parent da08606 commit 9568583
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
53 changes: 52 additions & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe("Gatsby Node Hook", () => {
},
],
};

const helpers = {
createNodeId: jest.fn(),
createContentDigest: jest.fn(),
Expand All @@ -36,4 +35,56 @@ describe("Gatsby Node Hook", () => {
done();
});
});

test("should run when sites is undefined", (done) => {
// Arrange
const config = {
host: "iteamnm.sharepoint.com",
appId: process.env.AppId,
appSecret: process.env.AppSecret,
tenantId: process.env.TenantId,
};
const helpers = {
createNodeId: jest.fn(),
createContentDigest: jest.fn(),
actions: {
createNode: jest.fn(),
},
};

// Act & Assert
sourceNodes(helpers, config, () => {
expect(helpers.actions.createNode).not.toHaveBeenCalled();
done();
});
});

test("should run when lists is undefined", (done) => {
// Arrange
const config = {
host: "iteamnm.sharepoint.com",
appId: process.env.AppId,
appSecret: process.env.AppSecret,
tenantId: process.env.TenantId,
sites: [
{
name: "gatsby-source-sharepoint-online",
relativePath: "sites/gatsby-source-sharepoint-online",
},
],
};
const helpers = {
createNodeId: jest.fn(),
createContentDigest: jest.fn(),
actions: {
createNode: jest.fn(),
},
};

// Act & Assert
sourceNodes(helpers, config, () => {
expect(helpers.actions.createNode).toHaveBeenCalledTimes(1);
done();
});
});
});
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.sourceNodes = function sourceNodes(helpers, config, callback) {
createContentDigest,
actions: { createNode },
} = helpers;
const { plugins, host, sites, ...creds } = config;
const { plugins, host, sites = [], ...creds } = config;
const client = createClient(creds);

/**
Expand All @@ -29,6 +29,8 @@ exports.sourceNodes = function sourceNodes(helpers, config, callback) {
throw new Error("site must be defined.");
}

const { lists = [] } = site;

/**
* Process a site list.
* @param {string} siteId The id of the site this list belongs to.
Expand Down Expand Up @@ -76,7 +78,7 @@ exports.sourceNodes = function sourceNodes(helpers, config, callback) {
},
});

return Promise.all(site.lists.map((list) => processList(siteId, list)));
return Promise.all(lists.map((list) => processList(siteId, list)));
});
};

Expand Down

0 comments on commit 9568583

Please sign in to comment.