Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Adding ability to sort the import map alphabetically #122

Merged
merged 3 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ function modifyLock(env, modifierFunc) {
}

exports.modifyImportMap = function (env, newValues) {
const { services: newImports, scopes: newScopes } = newValues;
const { services, scopes } = newValues;

const alphabetical = !!getConfig().alphabetical;
const newImports =
services && typeof services === "object" && alphabetical
? sortObjectAlphabeticallyByKeys(services)
: services;
const newScopes =
scopes && typeof scopes === "object" && alphabetical
? sortObjectAlphabeticallyByKeys(scopes)
: scopes;

// either imports or scopes have to be defined
if (newImports || newScopes) {
Expand Down Expand Up @@ -135,9 +145,25 @@ exports.modifyService = function (
map[serviceName + "/"] = address;
}
}

return json;
const alphabetical = !!getConfig().alphabetical;
if (alphabetical) {
return {
imports: sortObjectAlphabeticallyByKeys(json.imports),
scopes: sortObjectAlphabeticallyByKeys(json.scopes),
};
} else {
return json;
}
});
};

exports.getEmptyManifest = getEmptyManifest;

function sortObjectAlphabeticallyByKeys(unordered) {
return Object.keys(unordered)
.sort()
.reduce((obj, key) => {
obj[key] = unordered[key];
return obj;
}, {});
}
116 changes: 116 additions & 0 deletions test/alphabetical-import-map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const request = require("supertest");
const { app, setConfig } = require("../src/web-server");
const {
resetManifest: resetMemoryManifest,
} = require("../src/io-methods/memory");

describe(`alphabetically sorted`, () => {
beforeAll(() => {
setConfig({
manifestFormat: "importmap",
alphabetical: true,
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
const setupRequest = request(app)
.patch("/import-map.json")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
imports: {
c: "/c-1.mjs",
b: "/b-1.mjs",
},
})
.expect(200)
.expect("Content-Type", /json/);
return setupRequest.then((response) => {
expect(JSON.stringify(response.body)).toBe(
`{"imports":{"b":"/b-1.mjs","c":"/c-1.mjs"},"scopes":{}}`
);
});
});

it(`should place the import into the map alphabetically instead of just at the end`, async () => {
const response = await request(app)
.patch("/services")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
service: "a",
url: "/a-1-updated.mjs",
})
.expect(200)
.expect("Content-Type", /json/);

expect(JSON.stringify(response.body.imports)).toBe(
`{"a":"/a-1-updated.mjs","b":"/b-1.mjs","c":"/c-1.mjs"}`
);
});
});

describe(`not alphabetically sorted`, () => {
beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
const setupRequest = request(app)
.patch("/import-map.json")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
imports: {
c: "/c-1.mjs",
b: "/b-1.mjs",
},
})
.expect(200)
.expect("Content-Type", /json/);
return setupRequest.then((response) => {
expect(JSON.stringify(response.body)).toBe(
`{"imports":{"c":"/c-1.mjs","b":"/b-1.mjs"},"scopes":{}}`
);
});
});

it(`should place the import into the map alphabetically instead of just at the end`, async () => {
TheMcMurder marked this conversation as resolved.
Show resolved Hide resolved
const response = await request(app)
.patch("/services")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
service: "a",
url: "/a-1-updated.mjs",
})
.expect(200)
.expect("Content-Type", /json/);

expect(JSON.stringify(response.body.imports)).toBe(
`{"c":"/c-1.mjs","b":"/b-1.mjs","a":"/a-1-updated.mjs"}`
);
});
});
30 changes: 16 additions & 14 deletions test/import-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const {
resetManifest: resetMemoryManifest,
} = require("../src/io-methods/memory");

beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
describe(`/import-map.json`, () => {
let errorSpy;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this errorSpy so the tests wouldn't actually error to the console and clutter up the output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to scope it as well so I moved the beforeAll and beforeEach blocks into the describe block

beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});
beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
errorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
errorSpy.mockClear();
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
});

describe(`/import-map.json`, () => {
it(`does not return anything when it's not setup yet.`, async () => {
const response = await request(app)
.get("/import-map.json")
Expand Down