TestUtils.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { OpenSheetMusicDisplay } from "../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
  2. /**
  3. * This class collects useful methods to interact with test data.
  4. * During tests, XML and MXL documents are preprocessed by karma,
  5. * and this is some helper code to retrieve them.
  6. */
  7. export class TestUtils {
  8. public static getScore(name: string): Document {
  9. const path: string = "test/data/" + name;
  10. return ((window as any).__xml__)[path];
  11. }
  12. public static getMXL(scoreName: string): string {
  13. const path: string = "test/data/" + scoreName;
  14. return ((window as any).__raw__)[path];
  15. }
  16. public static getDivElement(document: Document): HTMLElement {
  17. const div: HTMLElement = document.createElement("div");
  18. const body: HTMLElement = document.getElementsByTagName("body")[0];
  19. body.appendChild(div);
  20. return div;
  21. }
  22. /**
  23. * Retrieve from a XML document the first element with name "score-partwise"
  24. * @param doc is the XML Document
  25. * @returns {Element}
  26. */
  27. public static getPartWiseElement(doc: Document): Element {
  28. const nodes: NodeList = doc.childNodes;
  29. for (let i: number = 0, length: number = nodes.length; i < length; i += 1) {
  30. const node: Node = nodes[i];
  31. if (node.nodeType === Node.ELEMENT_NODE && node.nodeName.toLowerCase() === "score-partwise") {
  32. return <Element>node;
  33. }
  34. }
  35. }
  36. public static createOpenSheetMusicDisplay(div: HTMLElement): OpenSheetMusicDisplay {
  37. return new OpenSheetMusicDisplay(div, {autoResize: false});
  38. }
  39. }