Skip to content

Commit

Permalink
Move clock mocks into beforeEach in case test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawrence Owen committed Feb 10, 2025
1 parent 2291824 commit 4059cc7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 74 deletions.
9 changes: 6 additions & 3 deletions test/ReactViewModels/ViewStateSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ describe("ViewState", function () {
});
});
describe("tour and trainer interaction", function () {
it("disables trainer bar if turning on tour", function () {
beforeEach(function () {
jasmine.clock().install();

});
afterEach(function () {
jasmine.clock().uninstall();
});
it("disables trainer bar if turning on tour", function () {
runInAction(() => {
viewState.setTrainerBarExpanded(true);
viewState.setTrainerBarShowingAllSteps(true);
Expand All @@ -123,7 +127,6 @@ describe("ViewState", function () {
expect(viewState.trainerBarExpanded).toEqual(false);
expect(viewState.trainerBarShowingAllSteps).toEqual(false);
expect(viewState.showTour).toEqual(true);
jasmine.clock().uninstall();
});
});

Expand Down
152 changes: 81 additions & 71 deletions test/ReactViews/Tour/TourPortalSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,81 +56,91 @@ describe("TourPortal", function () {
expect(testRenderer.root.findByType(TourPreface)).toBeDefined();
expect(() => testRenderer.root.findByType(TourExplanation)).toThrow();
});
it("renders something using the TourGrouping path under showPortal conditions", function () {
jasmine.clock().install();
const testRef: any = React.createRef();
const testRef2: any = React.createRef();
const testRef3: any = React.createRef();
act(() => {
testRenderer = createWithContexts(
viewState,
<div>
<div ref={testRef} />
<div ref={testRef2} />
<div ref={testRef3} />
<TourPortal />
</div>,
{
createNodeMock: (/* element: any */) => {
return {
// This is not compulsory as we still render if we
// can't get a rectangle, but we'll mock it anyway
getBoundingClientRect: () => ({
top: 0,
left: 0,
right: 0,
bottom: 0
})
};
// return document.createElement("div");
}
}
);
describe("With mocked timing", function () {
beforeEach(function () {
jasmine.clock().install();
});
runInAction(() => {
viewState.setTourIndex(0);
viewState.setShowTour(true);

viewState.updateAppRef("TestRef", testRef);
viewState.updateAppRef("TestRef2", testRef2);
viewState.updateAppRef("TestRef3", testRef3);
viewState.tourPoints = [
{
appRefName: "TestRef",
priority: 10,
content: "## Best friends\n\nMochi and neko are best friends"
},
{
appRefName: "TestRef2",
priority: 20,
content: "## Motivated by food\n\nNeko loves food"
},
{
appRefName: "TestRef3",
priority: 30,
content: "## Lazy\n\nThey like to lounge around all day"
}
];
});
act(() => {
testRenderer = createWithContexts(viewState, <TourPortal />);
afterEach(function () {
jasmine.clock().uninstall();
});
jasmine.clock().tick(animationDuration); // wait for workbench hide animation
// 3 test tour points
expect(testRenderer.root.findAllByType(TourExplanation).length).toBe(3);
it("renders something using the TourGrouping path under showPortal conditions", function () {
const testRef: any = React.createRef();
const testRef2: any = React.createRef();
const testRef3: any = React.createRef();
act(() => {
testRenderer = createWithContexts(
viewState,
<div>
<div ref={testRef} />
<div ref={testRef2} />
<div ref={testRef3} />
<TourPortal />
</div>,
{
createNodeMock: (/* element: any */) => {
return {
// This is not compulsory as we still render if we
// can't get a rectangle, but we'll mock it anyway
getBoundingClientRect: () => ({
top: 0,
left: 0,
right: 0,
bottom: 0
})
};
// return document.createElement("div");
}
}
);
});
runInAction(() => {
viewState.setTourIndex(0);
viewState.setShowTour(true);

// Remove one tour point and we should only have 2 left
// (e.g. if you add a tour point on the compass,
// this is triggered when compass disappears between 2D<->3D modes)
runInAction(() => {
viewState.deleteAppRef("TestRef");
});
act(() => {
testRenderer = createWithContexts(viewState, <TourPortal />);
viewState.updateAppRef("TestRef", testRef);
viewState.updateAppRef("TestRef2", testRef2);
viewState.updateAppRef("TestRef3", testRef3);
viewState.tourPoints = [
{
appRefName: "TestRef",
priority: 10,
content: "## Best friends\n\nMochi and neko are best friends"
},
{
appRefName: "TestRef2",
priority: 20,
content: "## Motivated by food\n\nNeko loves food"
},
{
appRefName: "TestRef3",
priority: 30,
content: "## Lazy\n\nThey like to lounge around all day"
}
];
});
act(() => {
testRenderer = createWithContexts(viewState, <TourPortal />);
});
jasmine.clock().tick(animationDuration); // wait for workbench hide animation
// 3 test tour points
expect(testRenderer.root.findAllByType(TourExplanation).length).toBe(
3
);

// Remove one tour point and we should only have 2 left
// (e.g. if you add a tour point on the compass,
// this is triggered when compass disappears between 2D<->3D modes)
runInAction(() => {
viewState.deleteAppRef("TestRef");
});
act(() => {
testRenderer = createWithContexts(viewState, <TourPortal />);
});
// 2 test tour points
expect(testRenderer.root.findAllByType(TourExplanation).length).toBe(
2
);
});
// 2 test tour points
expect(testRenderer.root.findAllByType(TourExplanation).length).toBe(2);
jasmine.clock().uninstall();
});
});
});
Expand Down

0 comments on commit 4059cc7

Please sign in to comment.