|
| 1 | +import { render, fireEvent, within } from "@testing-library/vue"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import ExperimentStatisticsContainer from "@/components/statistics/ExperimentStatisticsContainer.vue"; |
| 4 | +import Vue from "vue"; |
| 5 | +import BootstrapVue from "bootstrap-vue"; |
| 6 | +import VueFlatPickr from "vue-flatpickr-component"; |
| 7 | + |
| 8 | +Vue.use(BootstrapVue); |
| 9 | +Vue.use(VueFlatPickr); |
| 10 | + |
| 11 | +import { models, services, utils } from "django-airavata-api"; |
| 12 | +import ExperimentStatus from "django-airavata-api/static/django_airavata_api/js/models/ExperimentStatus"; |
| 13 | +jest.mock("django-airavata-api", () => { |
| 14 | + const originalModule = jest.requireActual("django-airavata-api"); |
| 15 | + return { |
| 16 | + __esModule: true, |
| 17 | + ...originalModule, |
| 18 | + // Mock just the RESTful service calls |
| 19 | + services: { |
| 20 | + ApplicationInterfaceService: { |
| 21 | + list: jest.fn(), |
| 22 | + }, |
| 23 | + ExperimentStatisticsService: { |
| 24 | + get: jest.fn(), |
| 25 | + }, |
| 26 | + ComputeResourceService: { |
| 27 | + namesList: jest.fn(), |
| 28 | + }, |
| 29 | + ExperimentSearchService: { |
| 30 | + list: jest.fn(), |
| 31 | + }, |
| 32 | + ExperimentService: { |
| 33 | + retrieve: jest.fn(), |
| 34 | + }, |
| 35 | + FullExperimentService: { |
| 36 | + retrieve: jest.fn(), |
| 37 | + }, |
| 38 | + }, |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +// beforeEach(() => { |
| 43 | +// jest.resetAllMocks(); |
| 44 | +// }); |
| 45 | + |
| 46 | +test("load experiment by job id when job id matches unique experiment", async () => { |
| 47 | + const spinner = document.createElement("div"); |
| 48 | + spinner.id = "airavata-spinner"; |
| 49 | + document.body.appendChild(spinner); |
| 50 | + |
| 51 | + // Service call mocks |
| 52 | + services.ApplicationInterfaceService.list.mockResolvedValue([]); |
| 53 | + services.ExperimentStatisticsService.get.mockResolvedValue( |
| 54 | + new utils.PaginationIterator( |
| 55 | + { |
| 56 | + count: 0, |
| 57 | + next: null, |
| 58 | + previous: null, |
| 59 | + results: { |
| 60 | + allExperimentCount: 0, |
| 61 | + completedExperimentCount: 0, |
| 62 | + cancelledExperimentCount: 0, |
| 63 | + failedExperimentCount: 0, |
| 64 | + createdExperimentCount: 0, |
| 65 | + runningExperimentCount: 0, |
| 66 | + allExperiments: [], |
| 67 | + completedExperiments: [], |
| 68 | + failedExperiments: [], |
| 69 | + cancelledExperiments: [], |
| 70 | + createdExperiments: [], |
| 71 | + runningExperiments: [], |
| 72 | + }, |
| 73 | + limit: 50, |
| 74 | + offset: 0, |
| 75 | + }, |
| 76 | + models.ExperimentStatistics |
| 77 | + ) |
| 78 | + ); |
| 79 | + services.ComputeResourceService.namesList.mockResolvedValue([]); |
| 80 | + services.ExperimentSearchService.list.mockResolvedValue( |
| 81 | + new utils.PaginationIterator( |
| 82 | + { |
| 83 | + count: 1, |
| 84 | + next: null, |
| 85 | + previous: null, |
| 86 | + results: [{ experimentId: "test-experiment-id" }], |
| 87 | + }, |
| 88 | + models.ExperimentSummary |
| 89 | + ) |
| 90 | + ); |
| 91 | + // Mock just enough of Experiment and FullExperiment to get ExperimentDetailsView to render |
| 92 | + const experiment = new models.Experiment({ |
| 93 | + experimentId: "test-experiment-id", |
| 94 | + experimentName: "Test Experiment", |
| 95 | + creationTime: Date.now(), |
| 96 | + experimentStatus: [ |
| 97 | + new ExperimentStatus({ |
| 98 | + timeOfStateChange: Date.now(), |
| 99 | + state: models.ExperimentState.COMPLETED, |
| 100 | + }), |
| 101 | + ], |
| 102 | + }); |
| 103 | + services.ExperimentService.retrieve.mockResolvedValue(experiment); |
| 104 | + services.FullExperimentService.retrieve.mockResolvedValue( |
| 105 | + new models.FullExperiment({ |
| 106 | + experimentId: "test-experiment-id", |
| 107 | + experiment, |
| 108 | + }) |
| 109 | + ); |
| 110 | + |
| 111 | + // jsdom doesn't implement scrollIntoView so just provide a stubbed implementation |
| 112 | + Element.prototype.scrollIntoView = jest.fn(); |
| 113 | + |
| 114 | + // The render method returns a collection of utilities to query your component. |
| 115 | + const { findByText, findByPlaceholderText } = render( |
| 116 | + ExperimentStatisticsContainer |
| 117 | + ); |
| 118 | + |
| 119 | + const byJobIDTab = await findByText("By Job ID"); |
| 120 | + |
| 121 | + await fireEvent.click(byJobIDTab); |
| 122 | + |
| 123 | + const jobIDInputField = await findByPlaceholderText("Job ID"); |
| 124 | + |
| 125 | + await fireEvent.update(jobIDInputField, "12345"); |
| 126 | + |
| 127 | + const loadButton = await within(jobIDInputField.parentElement).findByText( |
| 128 | + "Load" |
| 129 | + ); |
| 130 | + |
| 131 | + await fireEvent.click(loadButton); |
| 132 | + |
| 133 | + // The job's tab has the job id instead of the normal experiment name |
| 134 | + const jobTab = await findByText("Job 12345"); |
| 135 | + |
| 136 | + expect(jobTab).toBeVisible(); |
| 137 | + |
| 138 | + // Double check that the experiment services were called to load the experiment |
| 139 | + expect(services.ExperimentService.retrieve).toHaveBeenCalledWith({ |
| 140 | + lookup: experiment.experimentId, |
| 141 | + }); |
| 142 | + expect(services.FullExperimentService.retrieve).toHaveBeenCalledWith({ |
| 143 | + lookup: experiment.experimentId, |
| 144 | + }); |
| 145 | +}); |
0 commit comments