it(`default settings work properly`, async () => { internals.writeFile = jest.fn() internals.writeFile.mockResolvedValue(true) const graphql = jest.fn() graphql.mockResolvedValue({ data: { site: { siteMetadata: { siteUrl: `http://dummy.url`, }, }, allSitePage: { edges: [ { node: { path: `/page-1`, }, }, { node: { path: `/page-2`, }, }, ], }, }, }) await onPostBuild({ graphql, pathPrefix }, {}) const [filePath, contents] = internals.writeFile.mock.calls[0] expect(filePath).toEqual(path.join(`public`, `sitemap.xml`)) expect(contents).toMatchSnapshot() })
it(`default settings work properly`, async () => { fs.writeFile = jest.fn() fs.writeFile.mockResolvedValue(true) const graphql = jest.fn() graphql.mockResolvedValue({ data: { site: { siteMetadata: { title: `a sample title`, description: `a description`, siteUrl: `http://dummy.url/`, }, }, allMarkdownRemark: { edges: [ { node: { fields: { slug: `a-slug`, }, excerpt: `post description`, }, }, ], }, }, }) await onPostBuild({ graphql }, {}) const [filePath, contents] = fs.writeFile.mock.calls[0] expect(filePath).toEqual(path.join(`public`, `rss.xml`)) expect(contents).toMatchSnapshot() })
it(`does not mutate base query when merging`, async () => { fs.writeFile = jest.fn() fs.writeFile.mockResolvedValue() const siteQuery = { data: { site: { siteMetadata: { title: `Hello World`, }, }, }, } const markdownQuery = { data: { allMarkdownRemark: { edges: [ { node: { fields: { slug: `/hello-world`, }, frontmatter: { title: `Hello World`, }, }, }, ], }, }, } const graphql = jest .fn() .mockResolvedValueOnce(siteQuery) .mockResolvedValueOnce(markdownQuery) const options = { query: `{}`, feeds: [ { output: `rss.xml`, query: `{ firstMarkdownQuery }`, }, ], } await onPostBuild({ graphql }, options) expect(siteQuery).toEqual({ data: { site: expect.any(Object), }, }) })
beforeEach(() => { graphql = jest.fn() graphql.mockResolvedValue(queryResult) internals.renameFile = jest.fn() internals.renameFile.mockResolvedValue(true) internals.writeFile = jest.fn() internals.writeFile.mockResolvedValue(true) fs.createWriteStream.mockReset() fs.createWriteStream.mockReturnValue({ once: jest.fn((event, cb) => cb()), write: jest.fn(), end: jest.fn(), }) fs.statSync.mockReset() fs.statSync.mockReturnValue({ isDirectory: jest.fn(() => true), }) })
it(`custom query runs`, async () => { internals.writeFile = jest.fn() internals.writeFile.mockResolvedValue(true) const graphql = jest.fn() graphql.mockResolvedValue({ data: { site: { siteMetadata: { siteUrl: `http://dummy.url`, }, }, allSitePage: { edges: [ { node: { path: `/page-1`, }, }, { node: { path: `/post/exclude-page`, }, }, ], }, }, }) const customQuery = ` { site { siteMetadata { siteUrl } } allSitePage { edges { node { path } } } }` const options = { output: `custom-sitemap.xml`, serialize: ({ site, allSitePage }) => allSitePage.edges.map(edge => { return { url: site.siteMetadata.siteUrl + `/post` + edge.node.path, changefreq: `weekly`, priority: 0.8, } }), exclude: [`/post/exclude-page`], query: customQuery, } await onPostBuild({ graphql, pathPrefix }, options) const [filePath, contents] = internals.writeFile.mock.calls[0] expect(filePath).toEqual(path.join(`public`, `custom-sitemap.xml`)) expect(contents).toMatchSnapshot() expect(graphql).toBeCalledWith(customQuery) })
it(`custom query runs`, async () => { fs.writeFile = jest.fn() fs.writeFile.mockResolvedValue(true) const graphql = jest.fn() graphql.mockResolvedValue({ data: { site: { siteMetadata: { title: `a sample title`, description: `a description`, siteUrl: `http://dummy.url/`, }, }, allMarkdownRemark: { edges: [ { node: { frontmatter: { path: `a-custom-path`, }, excerpt: `post description`, }, }, { node: { frontmatter: { path: `another-custom-path`, }, excerpt: `post description`, }, }, ], }, }, }) const customQuery = ` { allMarkdownRemark( limit: 1000, ) { edges { node { frontmatter { path } excerpt } } } } ` const options = { feeds: [ { output: `rss_new.xml`, serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.map(edge => { return { ...edge.node.frontmatter, description: edge.node.excerpt, url: site.siteMetadata.siteUrl + edge.node.frontmatter.path, } }), query: customQuery, }, ], } await onPostBuild({ graphql }, options) const [filePath, contents] = fs.writeFile.mock.calls[0] expect(filePath).toEqual(path.join(`public`, `rss_new.xml`)) expect(contents).toMatchSnapshot() expect(graphql).toBeCalledWith(customQuery) })