Skip to content

Commit

Permalink
feat: coverage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiAlexandruParaschiv committed Aug 12, 2024
1 parent d7ba18c commit bd74df4
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"lcov",
"text"
],
"check-coverage": false,
"check-coverage": true,
"lines": 100,
"branches": 100,
"statements": 100,
Expand Down
9 changes: 6 additions & 3 deletions src/sitemap/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ async function checkCommonSitemapUrls(urls) {
return response.ok ? url : null;
});
const results = await Promise.all(fetchPromises);
// Returns only URLs that exist
return results.filter((url) => url !== null);
}

Expand Down Expand Up @@ -278,10 +279,12 @@ export async function findSitemap(inputUrl) {

// check if URLs from each sitemap exist and remove entries if none exist
if (Object.entries(extractedPaths).length > 0) {
const extractedSitemapUrlEntries = Object.keys(extractedPaths);
for (const s of extractedSitemapUrlEntries) {
const extractedSitemapUrls = Object.keys(extractedPaths);
for (const s of extractedSitemapUrls) {
const urlsToCheck = extractedPaths[s];
// eslint-disable-next-line no-await-in-loop
const existingPages = await checkCommonSitemapUrls(extractedSitemapUrlEntries[s]);
const existingPages = await checkCommonSitemapUrls(urlsToCheck);

if (existingPages.length === 0) {
delete extractedPaths[s];
} else {
Expand Down
191 changes: 156 additions & 35 deletions test/audits/sitemap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ describe('Sitemap Audit', () => {
+ `<sitemap><loc>${url}/sitemap_bar.xml</loc></sitemap>\n`
+ '</sitemapindex>';

let topPagesResponse;

beforeEach('setup', () => {
context = new MockContextBuilder()
.withSandbox(sandbox)
Expand All @@ -78,29 +76,6 @@ describe('Sitemap Audit', () => {
nock(url)
.get('/sitemap_bar.xml')
.reply(200, sampleSitemapTwo);

topPagesResponse = {
result: {
pages: [
{
url: `${url}/foo`,
sum_traffic: 100,
},
{
url: `${url}/bar`,
sum_traffic: 200,
},
{
url: `${url}/baz`,
sum_traffic: 300,
},
{
url: `${url}/cux`,
sum_traffic: 400,
},
],
},
};
});

afterEach(() => {
Expand All @@ -110,11 +85,35 @@ describe('Sitemap Audit', () => {
});

describe('sitemapAuditRunner', () => {
it.skip('runs successfully for sitemaps extracted from robots.txt', async () => {
it('runs successfully for sitemaps extracted from robots.txt', async () => {
nock(url)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap_foo.xml\nSitemap: ${url}/sitemap_bar.xml`);

nock(url)
.head('/sitemap_foo.xml')
.reply(200);

nock(url)
.head('/sitemap_bar.xml')
.reply(200);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

nock(url)
.head('/baz')
.reply(200);

nock(url)
.head('/cux')
.reply(200);

const result = await sitemapAuditRunner(url, context);
expect(result).to.eql({
auditResult: {
Expand All @@ -133,13 +132,39 @@ describe('Sitemap Audit', () => {
});
});

it.skip('runs successfully for sitemap extracted from robots.txt through sitemap index', async () => {
it('runs successfully for sitemap extracted from robots.txt through sitemap index', async () => {
nock(url)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap_index.xml`);

nock(url)
.get('/sitemap_index.xml')
.reply(200, sitemapIndex);

nock(url)
.head('/sitemap_foo.xml')
.reply(200);

nock(url)
.head('/sitemap_bar.xml')
.reply(200);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

nock(url)
.head('/baz')
.reply(200);

nock(url)
.head('/cux')
.reply(200);

const result = await sitemapAuditRunner(url, context);
expect(result).to.eql({
auditResult: {
Expand All @@ -158,18 +183,35 @@ describe('Sitemap Audit', () => {
});
});

it.skip('runs successfully for text sitemap extracted from robots.txt', async () => {
it('runs successfully for text sitemap extracted from robots.txt', async () => {
nock(url)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap_foo.txt\nSitemap: ${url}/sitemap_bar.txt`);

nock(url)
.get('/sitemap_foo.txt')
.reply(200, `${url}/foo\n${url}/bar`, { 'content-type': 'text/plain' });

nock(url)
.get('/sitemap_bar.txt')
.reply(200, `${url}/baz\n${url}/cux`, { 'content-type': 'text/plain' });

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

nock(url)
.head('/baz')
.reply(200);

nock(url)
.head('/cux')
.reply(200);

const result = await sitemapAuditRunner(url, context);
expect(result).to.eql({
auditResult: {
Expand All @@ -188,7 +230,7 @@ describe('Sitemap Audit', () => {
});
});

it.skip('runs successfully for common sitemap url when robots.txt is not available', async () => {
it('runs successfully for common sitemap url when robots.txt is not available', async () => {
nock(url)
.get('/robots.txt')
.reply(404);
Expand All @@ -205,6 +247,14 @@ describe('Sitemap Audit', () => {
.get('/sitemap.xml')
.reply(200, sampleSitemap);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

const result = await sitemapAuditRunner(url, context);
expect(result).to.eql({
auditResult: {
Expand Down Expand Up @@ -442,7 +492,33 @@ describe('Sitemap Audit', () => {
}]);
});

it.skip('should return success when sitemap is found in robots.txt', async () => {
it('should delete extracted paths when no valid pages exist', async () => {
nock(url)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap.xml`);

nock(url)
.get('/sitemap.xml')
.reply(200, sampleSitemap);

nock(url)
.head('/foo')
.reply(404);

nock(url)
.head('/bar')
.reply(404);

const result = await findSitemap(url);

expect(result.success).to.equal(false);
expect(result.reasons).to.deep.include({
value: 'No valid paths extracted from sitemaps.',
error: ERROR_CODES.NO_PATHS_IN_SITEMAP,
});
});

it('should return success when sitemap is found in robots.txt', async () => {
nock(url)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap.xml`);
Expand All @@ -451,14 +527,22 @@ describe('Sitemap Audit', () => {
.get('/sitemap.xml')
.reply(200, sampleSitemap);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

const result = await findSitemap(url);
expect(result.success).to.equal(true);
expect(result.paths).to.deep.equal({
[`${url}/sitemap.xml`]: [`${url}/foo`, `${url}/bar`],
});
});

it.skip('should return success when sitemap.xml is found', async () => {
it('should return success when sitemap.xml is found', async () => {
nock(url)
.get('/robots.txt')
.reply(200, 'Allow: /');
Expand All @@ -475,14 +559,22 @@ describe('Sitemap Audit', () => {
.get('/sitemap.xml')
.reply(200, sampleSitemap);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

const result = await findSitemap('https://some-domain.adobe');
expect(result.success).to.equal(true);
expect(result.paths).to.deep.equal({
[`${url}/sitemap.xml`]: [`${url}/foo`, `${url}/bar`],
});
});

it.skip('should return success when sitemap_index.xml is found', async () => {
it('should return success when sitemap_index.xml is found', async () => {
nock(url)
.get('/robots.txt')
.reply(200, 'Allow: /');
Expand All @@ -499,6 +591,30 @@ describe('Sitemap Audit', () => {
.get('/sitemap_index.xml')
.reply(200, sitemapIndex);

nock(url)
.head('/sitemap_foo.xml')
.reply(200);

nock(url)
.head('/sitemap_bar.xml')
.reply(200);

nock(url)
.head('/foo')
.reply(200);

nock(url)
.head('/bar')
.reply(200);

nock(url)
.head('/baz')
.reply(200);

nock(url)
.head('/cux')
.reply(200);

const result = await findSitemap(url);
expect(result.success).to.equal(true);
expect(result.paths).to.deep.equal({
Expand All @@ -507,7 +623,7 @@ describe('Sitemap Audit', () => {
});
});

it.skip('should return success when sitemap paths have www', async () => {
it('should return success when sitemap paths have www', async () => {
nock(`${protocol}://www.${domain}`)
.get('/robots.txt')
.reply(200, `Sitemap: ${url}/sitemap.xml`);
Expand All @@ -516,8 +632,13 @@ describe('Sitemap Audit', () => {
.get('/sitemap.xml')
.reply(200, sampleSitemapMoreUrlsWWW);

topPagesResponse.result.pages[0].url = `${protocol}://www.${domain}/foo`;
topPagesResponse.result.pages[1].url = `${protocol}://www.${domain}/bar`;
nock(`${protocol}://www.${domain}`)
.head('/foo')
.reply(200);

nock(`${protocol}://www.${domain}`)
.head('/bar')
.reply(200);

const result = await findSitemap(`${protocol}://www.${domain}`);
expect(result.success).to.equal(true);
Expand Down

0 comments on commit bd74df4

Please sign in to comment.