Skip to content

Commit 29c6014

Browse files
Robert JacksonChris Garrett
authored andcommitted
Split apart and add unit tests for getTelemetryFor. (#127)
1 parent 008e0e7 commit 29c6014

4 files changed

Lines changed: 53 additions & 11 deletions

File tree

.eslintrc.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ module.exports = {
1111
plugins: ["prettier"],
1212
rules: {
1313
"prettier/prettier": "error"
14-
}
14+
},
15+
overrides: [
16+
{
17+
files: ['**/*.test.js'],
18+
env: {
19+
jest: true
20+
}
21+
}
22+
]
1523
};

transforms/helpers/parse-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
getOptions,
77
startsWithUpperCaseLetter
88
} = require("./util");
9-
const getTelemetryFor = require("./util/get-telemetry-for");
9+
const { getTelemetryFor } = require("./util/get-telemetry-for");
1010
const {
1111
hasValidProps,
1212
isFileOfType,

transforms/helpers/util/get-telemetry-for.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const cache = require("../../../lib/cache");
66
const telemetry = cache.has("telemetry")
77
? JSON.parse(cache.get("telemetry").value)
88
: {};
9-
const addonFilePaths = {};
9+
const ADDON_PATHS = {};
1010

1111
let packagePaths = walkSync("./", {
1212
globs: ["**/package.json"],
@@ -16,34 +16,51 @@ let packagePaths = walkSync("./", {
1616
for (let packagePath of packagePaths) {
1717
let { name } = fs.readJsonSync(packagePath);
1818

19-
addonFilePaths[path.dirname(path.resolve(".", packagePath))] = name;
19+
ADDON_PATHS[path.dirname(path.resolve(".", packagePath))] = name;
2020
}
2121

2222
/**
23-
* Get the runtime data for the file being transformed
23+
* Transforms a literal "on disk" path to a "module path".
2424
*
25-
* @param {String} filePath Absolute path of the file to read data from
26-
* @returns {Object} Runtime configuration object
25+
* @param {String} filePath the path on disk (from current working directory)
26+
* @returns {String} The in-browser module path for the specified filePath
2727
*/
28-
module.exports = function getTelemetryData(filePath) {
28+
function getModulePathFor(filePath, addonPaths = ADDON_PATHS) {
2929
let fileSegments = filePath.split("/");
3030
let addonSegments = [];
3131

3232
while (fileSegments.length > 0) {
3333
addonSegments.push(fileSegments.shift());
3434

35-
if (addonFilePaths[addonSegments.join("/")]) {
35+
if (addonPaths[addonSegments.join("/")]) {
3636
break;
3737
}
3838
}
3939

4040
let addonFilePath = addonSegments.join("/");
41-
let addonName = addonFilePaths[addonFilePath];
41+
let addonName = addonPaths[addonFilePath];
4242

4343
let relativeFilePath = fileSegments
4444
.join("/")
4545
.replace(/^(addon|app)\//, "")
4646
.replace(/\.[^/.]+$/, "");
4747

48-
return telemetry[`${addonName}/${relativeFilePath}`];
48+
return `${addonName}/${relativeFilePath}`;
49+
}
50+
51+
/**
52+
* Get the runtime data for the file being transformed
53+
*
54+
* @param {String} filePath Absolute path of the file to read data from
55+
* @returns {Object} Runtime configuration object
56+
*/
57+
function getTelemetryFor(filePath) {
58+
let modulePath = getModulePathFor(filePath);
59+
60+
return telemetry[modulePath];
61+
}
62+
63+
module.exports = {
64+
getTelemetryFor,
65+
getModulePathFor
4966
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { getModulePathFor } = require("./get-telemetry-for");
2+
3+
describe("getModulePathFor", () => {
4+
test("accesses telemetry data for the specified app module", () => {
5+
const addonPaths = {
6+
"/User/whomever/voyager-web/lib/invitation-platform":
7+
"invitation-platform"
8+
};
9+
10+
expect(
11+
getModulePathFor(
12+
"/User/whomever/voyager-web/lib/invitation-platform/addon/components/fuse-limit-alert",
13+
addonPaths
14+
)
15+
).toEqual("invitation-platform/components/fuse-limit-alert");
16+
});
17+
});

0 commit comments

Comments
 (0)