TargetingDocs.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {ASRouterTargeting} from "lib/ASRouterTargeting.jsm";
  2. import docs from "content-src/asrouter/docs/targeting-attributes.md";
  3. // The following targeting parameters are either deprecated or should not be included in the docs for some reason.
  4. const SKIP_DOCS = [];
  5. // These are extra message context attributes via ASRouter.jsm
  6. const MESSAGE_CONTEXT_ATTRIBUTES = [
  7. "previousSessionEnd",
  8. "trailheadInterrupt",
  9. "trailheadTriplet",
  10. ];
  11. function getHeadingsFromDocs() {
  12. const re = /### `(\w+)`/g;
  13. const found = [];
  14. let match = 1;
  15. while (match) {
  16. match = re.exec(docs);
  17. if (match) {
  18. found.push(match[1]);
  19. }
  20. }
  21. return found;
  22. }
  23. function getTOCFromDocs() {
  24. const re = /## Available attributes\n+([^]+)\n+## Detailed usage/;
  25. const sectionMatch = docs.match(re);
  26. if (!sectionMatch) {
  27. return [];
  28. }
  29. const [, listText] = sectionMatch;
  30. const re2 = /\[(\w+)\]/g;
  31. const found = [];
  32. let match = 1;
  33. while (match) {
  34. match = re2.exec(listText);
  35. if (match) {
  36. found.push(match[1]);
  37. }
  38. }
  39. return found;
  40. }
  41. describe("ASRTargeting docs", () => {
  42. const DOCS_TARGETING_HEADINGS = getHeadingsFromDocs();
  43. const DOCS_TOC = getTOCFromDocs();
  44. const ASRTargetingAttributes = [
  45. ...Object.keys(ASRouterTargeting.Environment).filter(attribute => !SKIP_DOCS.includes(attribute)),
  46. ...MESSAGE_CONTEXT_ATTRIBUTES,
  47. ];
  48. describe("All targeting params documented in targeting-attributes.md", () => {
  49. for (const targetingParam of ASRTargetingAttributes) {
  50. // If this test is failing, you probably forgot to add docs to content-src/asrouter/targeting-attributes.md
  51. // for a new targeting attribute, or you forgot to put it in the table of contents up top.
  52. it(`should have docs and table of contents entry for ${targetingParam}`, () => {
  53. assert.include(DOCS_TARGETING_HEADINGS, targetingParam, `Didn't find the heading: ### \`${targetingParam}\``);
  54. assert.include(DOCS_TOC, targetingParam, `Didn't find a table of contents entry for ${targetingParam}`);
  55. });
  56. }
  57. });
  58. describe("No extra attributes in targeting-attributes.md", () => {
  59. for (const targetingParam of DOCS_TARGETING_HEADINGS) {
  60. // If this test is failing, you might has spelled something wrong or removed a targeting param without
  61. // removing its docs.
  62. it(`should have an implementation for ${targetingParam} in ASRouterTargeting.Environment`, () => {
  63. assert.include(ASRTargetingAttributes, targetingParam, `Didn't find an implementation for ${targetingParam}`);
  64. });
  65. }
  66. });
  67. });