Skip to content

Commit

Permalink
Get tests passing again
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Jul 10, 2019
1 parent 2e3c9ff commit f3ecdde
Show file tree
Hide file tree
Showing 42 changed files with 312 additions and 308 deletions.
10 changes: 5 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
moduleNameMapper: {
"\\.css$": "<rootDir>/modules/__mocks__/styleMock.js"
'entry-manifest': '<rootDir>/modules/__mocks__/entryManifest.js',
'\\.png$': '<rootDir>/modules/__mocks__/imageMock.js',
'\\.css$': '<rootDir>/modules/__mocks__/styleMock.js'
},
setupTestFrameworkScriptFile:
"<rootDir>/modules/__tests__/setupTestFramework.js",
testMatch: ["**/__tests__/*-test.js"],
testURL: "http://localhost/"
testMatch: ['**/__tests__/*-test.js'],
testURL: 'http://localhost/'
};
3 changes: 3 additions & 0 deletions modules/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "loose": true, "targets": "node 8" }]]
}
1 change: 1 addition & 0 deletions modules/__mocks__/entryManifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [];
1 change: 1 addition & 0 deletions modules/__mocks__/imageMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '';
19 changes: 19 additions & 0 deletions modules/__tests__/invalidPackageNames-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import request from 'supertest';

import createServer from '../createServer';

describe('Invalid package names', () => {
let server;
beforeEach(() => {
server = createServer();
});

it('are rejected', done => {
request(server)
.get('/_invalid/index.js')
.end((err, res) => {
expect(res.statusCode).toBe(403);
done();
});
});
});
20 changes: 20 additions & 0 deletions modules/__tests__/invalidQueryParams-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import request from 'supertest';

import createServer from '../createServer';

describe('Invalid query params', () => {
let server;
beforeEach(() => {
server = createServer();
});

it('redirect to the same path w/out those params', done => {
request(server)
.get('/d3?module&invalid-param')
.end((err, res) => {
expect(res.statusCode).toBe(302);
expect(res.headers.location).toBe('/d3?module');
done();
});
});
});
30 changes: 30 additions & 0 deletions modules/__tests__/legacyURLs-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import request from 'supertest';

import createServer from '../createServer';

describe('Legacy URLs', () => {
let server;
beforeEach(() => {
server = createServer();
});

it('redirect /_meta to ?meta', done => {
request(server)
.get('/_meta/react')
.end((err, res) => {
expect(res.statusCode).toBe(301);
expect(res.headers.location).toBe('/react?meta');
done();
});
});

it('redirect ?json to ?meta', done => {
request(server)
.get('/react?json')
.end((err, res) => {
expect(res.statusCode).toBe(301);
expect(res.headers.location).toBe('/react?meta');
done();
});
});
});
49 changes: 0 additions & 49 deletions modules/__tests__/server-test.js

This file was deleted.

14 changes: 7 additions & 7 deletions modules/actions/serveAutoIndexPage.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import semver from 'semver';

import AutoIndexApp from '../client/autoIndex/App';
import AutoIndexApp from '../client/autoIndex/App.js';

import createElement from './utils/createElement';
import createHTML from './utils/createHTML';
import createScript from './utils/createScript';
import getEntryPoint from './utils/getEntryPoint';
import getGlobalScripts from './utils/getGlobalScripts';
import MainTemplate from './utils/MainTemplate';
import MainTemplate from './utils/MainTemplate.js';
import createElement from './utils/createElement.js';
import createHTML from './utils/createHTML.js';
import createScript from './utils/createScript.js';
import getEntryPoint from './utils/getEntryPoint.js';
import getGlobalScripts from './utils/getGlobalScripts.js';

const doctype = '<!DOCTYPE html>';
const globalURLs =
Expand Down
8 changes: 4 additions & 4 deletions modules/actions/serveFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import serveAutoIndexPage from './serveAutoIndexPage';
import serveMetadata from './serveMetadata';
import serveModule from './serveModule';
import serveStaticFile from './serveStaticFile';
import serveAutoIndexPage from './serveAutoIndexPage.js';
import serveMetadata from './serveMetadata.js';
import serveModule from './serveModule.js';
import serveStaticFile from './serveStaticFile.js';

/**
* Send the file, JSON metadata, or HTML directory listing.
Expand Down
8 changes: 3 additions & 5 deletions modules/actions/serveHTMLModule.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import etag from 'etag';
import cheerio from 'cheerio';

import getContentTypeHeader from '../utils/getContentTypeHeader';
import rewriteBareModuleIdentifiers from '../utils/rewriteBareModuleIdentifiers';
import getContentTypeHeader from '../utils/getContentTypeHeader.js';
import rewriteBareModuleIdentifiers from '../utils/rewriteBareModuleIdentifiers.js';

export default function serveHTMLModule(req, res) {
try {
Expand Down Expand Up @@ -40,9 +40,7 @@ export default function serveHTMLModule(req, res) {
.status(500)
.type('text')
.send(
`Cannot generate module for ${req.packageSpec}${
req.filename
}\n\n${debugInfo}`
`Cannot generate module for ${req.packageSpec}${req.filename}\n\n${debugInfo}`
);
}
}
8 changes: 3 additions & 5 deletions modules/actions/serveJavaScriptModule.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import etag from 'etag';

import getContentTypeHeader from '../utils/getContentTypeHeader';
import rewriteBareModuleIdentifiers from '../utils/rewriteBareModuleIdentifiers';
import getContentTypeHeader from '../utils/getContentTypeHeader.js';
import rewriteBareModuleIdentifiers from '../utils/rewriteBareModuleIdentifiers.js';

export default function serveJavaScriptModule(req, res) {
try {
Expand Down Expand Up @@ -34,9 +34,7 @@ export default function serveJavaScriptModule(req, res) {
.status(500)
.type('text')
.send(
`Cannot generate module for ${req.packageSpec}${
req.filename
}\n\n${debugInfo}`
`Cannot generate module for ${req.packageSpec}${req.filename}\n\n${debugInfo}`
);
}
}
14 changes: 7 additions & 7 deletions modules/actions/serveMainPage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { renderToString, renderToStaticMarkup } from 'react-dom/server';

import MainApp from '../client/main/App';
import MainApp from '../client/main/App.js';

import createElement from './utils/createElement';
import createHTML from './utils/createHTML';
import createScript from './utils/createScript';
import getEntryPoint from './utils/getEntryPoint';
import getGlobalScripts from './utils/getGlobalScripts';
import MainTemplate from './utils/MainTemplate';
import MainTemplate from './utils/MainTemplate.js';
import createElement from './utils/createElement.js';
import createHTML from './utils/createHTML.js';
import createScript from './utils/createScript.js';
import getEntryPoint from './utils/getEntryPoint.js';
import getGlobalScripts from './utils/getGlobalScripts.js';

const doctype = '<!DOCTYPE html>';
const globalURLs =
Expand Down
2 changes: 1 addition & 1 deletion modules/actions/serveMetadata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import addLeadingSlash from '../utils/addLeadingSlash';
import addLeadingSlash from '../utils/addLeadingSlash.js';

function getMatchingEntries(entry, entries) {
const dirname = entry.name || '.';
Expand Down
4 changes: 2 additions & 2 deletions modules/actions/serveModule.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import serveHTMLModule from './serveHTMLModule';
import serveJavaScriptModule from './serveJavaScriptModule';
import serveHTMLModule from './serveHTMLModule.js';
import serveJavaScriptModule from './serveJavaScriptModule.js';

export default function serveModule(req, res) {
if (req.entry.contentType === 'application/javascript') {
Expand Down
2 changes: 1 addition & 1 deletion modules/actions/serveStaticFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import etag from 'etag';

import getContentTypeHeader from '../utils/getContentTypeHeader';
import getContentTypeHeader from '../utils/getContentTypeHeader.js';

export default function serveStaticFile(req, res) {
const tags = ['file'];
Expand Down
24 changes: 8 additions & 16 deletions modules/actions/utils/MainTemplate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import PropTypes from 'prop-types';

import e from './createElement';
import h from './createHTML';
import x from './createScript';
import e from './createElement.js';
import h from './createHTML.js';
import x from './createScript.js';

const promiseShim =
'window.Promise || document.write(\'\\x3Cscript src="/[email protected]/dist/es6-promise.min.js">\\x3C/script>\\x3Cscript>ES6Promise.polyfill()\\x3C/script>\')';
Expand All @@ -11,12 +11,12 @@ const fetchShim =
'window.fetch || document.write(\'\\x3Cscript src="/[email protected]/dist/fetch.umd.js">\\x3C/script>\')';

export default function MainTemplate({
title,
description,
favicon,
title = 'UNPKG',
description = 'The CDN for everything on npm',
favicon = '/favicon.ico',
data,
content,
elements
content = h(''),
elements = []
}) {
return e(
'html',
Expand Down Expand Up @@ -47,14 +47,6 @@ export default function MainTemplate({
);
}

MainTemplate.defaultProps = {
title: 'UNPKG',
description: 'The CDN for everything on npm',
favicon: '/favicon.ico',
content: h(''),
elements: []
};

if (process.env.NODE_ENV !== 'production') {
const htmlType = PropTypes.shape({
__html: PropTypes.string
Expand Down
14 changes: 8 additions & 6 deletions modules/actions/utils/cloudflare.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import fetch from 'isomorphic-fetch';
import invariant from 'invariant';

const cloudflareURL = 'https://api.cloudflare.com/client/v4';
const cloudflareEmail = process.env.CLOUDFLARE_EMAIL;
const cloudflareKey = process.env.CLOUDFLARE_KEY;

invariant(
cloudflareEmail,
'Missing the $CLOUDFLARE_EMAIL environment variable'
);
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') {
if (!cloudflareEmail) {
throw new Error('Missing the $CLOUDFLARE_EMAIL environment variable');
}

invariant(cloudflareKey, 'Missing the $CLOUDFLARE_KEY environment variable');
if (!cloudflareKey) {
throw new Error('Missing the $CLOUDFLARE_KEY environment variable');
}
}

function get(path, headers) {
return fetch(`${cloudflareURL}${path}`, {
Expand Down
4 changes: 2 additions & 2 deletions modules/actions/utils/createScript.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createElement from './createElement';
import createHTML from './createHTML';
import createElement from './createElement.js';
import createHTML from './createHTML.js';

export default function createScript(script) {
return createElement('script', {
Expand Down
1 change: 1 addition & 0 deletions modules/actions/utils/getEntryPoint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Virtual module id; see rollup.config.js
// eslint-disable-next-line import/no-unresolved
import entryManifest from 'entry-manifest';

export default function getEntryPoint(name, format) {
Expand Down
11 changes: 7 additions & 4 deletions modules/actions/utils/getGlobalScripts.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import invariant from 'invariant';

import createElement from './createElement';
import createElement from './createElement.js';

export default function getGlobalScripts(entryPoint, globalURLs) {
return entryPoint.globalImports.map(id => {
invariant(globalURLs[id], 'Missing global URL for id "%s"', id);
if (process.env.NODE_ENV !== 'production') {
if (!globalURLs[id]) {
throw new Error('Missing global URL for id "%s"', id);
}
}

return createElement('script', { src: globalURLs[id] });
});
}
10 changes: 3 additions & 7 deletions modules/actions/utils/getStats.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as cloudflare from './cloudflare.js';
import { getZones, getZoneAnalyticsDashboard } from './cloudflare.js';

function extractPublicInfo(data) {
return {
Expand Down Expand Up @@ -28,12 +28,8 @@ function extractPublicInfo(data) {
const DomainNames = ['unpkg.com', 'npmcdn.com'];

export default async function getStats(since, until) {
const zones = await cloudflare.getZones(DomainNames);
const dashboard = await cloudflare.getZoneAnalyticsDashboard(
zones,
since,
until
);
const zones = await getZones(DomainNames);
const dashboard = await getZoneAnalyticsDashboard(zones, since, until);

return {
timeseries: dashboard.timeseries.map(extractPublicInfo),
Expand Down
Loading

0 comments on commit f3ecdde

Please sign in to comment.