Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send initialization data in the NewReport message #2317

Merged
137 changes: 116 additions & 21 deletions frontend/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import React from "react"
import { shallow, mount, ReactWrapper } from "enzyme"
import { ForwardMsg } from "autogen/proto"
import { ForwardMsg, NewReport } from "autogen/proto"
import { IMenuItem } from "hocs/withS4ACommunication/types"
import { MetricsManager } from "./lib/MetricsManager"
import { getMetricsManagerForTest } from "./lib/MetricsManagerTestUtils"
Expand All @@ -28,17 +28,10 @@ import MainMenu from "./components/core/MainMenu"

const getProps = (extend?: Partial<Props>): Props => ({
screenCast: {
currentState: "OFF",
toggleRecordAudio: jest.fn(),
startRecording: jest.fn(),
stopRecording: jest.fn(),
fileName: "",
recording: false,
recordAudio: false,
countdown: -1,
startAnimation: false,
showRecordedDialog: false,
showScreencastDialog: false,
showUnsupportedDialog: false,
Comment on lines -34 to -41
Copy link
Contributor Author

@tconkling tconkling Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but I was making changes to App.tsx - none of these props exist

},
s4aCommunication: {
connect: jest.fn(),
Expand Down Expand Up @@ -84,7 +77,8 @@ describe("App", () => {
})

afterEach(() => {
SessionInfo.singleton = undefined
const UnsafeSessionInfo = SessionInfo as any
UnsafeSessionInfo.singleton = undefined
})

it("renders without crashing", () => {
Expand All @@ -93,22 +87,24 @@ describe("App", () => {
expect(wrapper.html()).not.toBeNull()
})

it("should reload when streamlit server version changes", () => {
it("reloads when streamlit server version changes", () => {
const props = getProps()
const wrapper = shallow(<App {...props} />)

window.location.reload = jest.fn()

const fwMessage = new ForwardMsg()

fwMessage.initialize = {
environmentInfo: {
streamlitVersion: "svv",
fwMessage.newReport = {
initialize: {
environmentInfo: {
streamlitVersion: "svv",
},
sessionId: "sessionId",
userInfo: {},
config: {},
sessionState: {},
},
sessionId: "sessionId",
userInfo: {},
config: {},
sessionState: {},
}

// @ts-ignore
Expand All @@ -117,7 +113,7 @@ describe("App", () => {
expect(window.location.reload).toHaveBeenCalled()
})

it("should start screencast recording when the MainMenu is clicked", () => {
it("starts screencast recording when the MainMenu is clicked", () => {
const props = getProps()
const wrapper = shallow(<App {...props} />)

Expand All @@ -135,7 +131,7 @@ describe("App", () => {
)
})

it("should stop screencast when esc is pressed", () => {
it("stops screencast when esc is pressed", () => {
const props = getProps()
const wrapper = shallow(<App {...props} />)

Expand All @@ -145,7 +141,7 @@ describe("App", () => {
expect(props.screenCast.stopRecording).toBeCalled()
})

it("should show s4aMenuItems", () => {
it("shows s4aMenuItems", () => {
const props = getProps({
s4aCommunication: {
connect: jest.fn(),
Expand All @@ -167,3 +163,102 @@ describe("App", () => {
])
})
})

describe("App.handleNewReport", () => {
const NEW_REPORT = new NewReport({
initialize: {
userInfo: {
installationId: "installationId",
installationIdV1: "installationIdV1",
installationIdV2: "installationIdV2",
email: "email",
},
config: {
sharingEnabled: false,
gatherUsageStats: false,
maxCachedMessageAge: 0,
mapboxToken: "mapboxToken",
allowRunOnSave: false,
},
environmentInfo: {
streamlitVersion: "streamlitVersion",
pythonVersion: "pythonVersion",
},
sessionState: {
runOnSave: false,
reportIsRunning: false,
},
sessionId: "sessionId",
commandLine: "commandLine",
},
})

afterEach(() => {
const UnsafeSessionInfo = SessionInfo as any
UnsafeSessionInfo.singleton = undefined
})

it("performs one-time initialization", () => {
const wrapper = shallow(<App {...getProps()} />)
const app = wrapper.instance()

const oneTimeInitialization = jest.spyOn(
app,
// @ts-ignore
"handleOneTimeInitialization"
)

expect(SessionInfo.isSet()).toBe(false)

// @ts-ignore
app.handleNewReport(NEW_REPORT)

expect(oneTimeInitialization).toHaveBeenCalledTimes(1)
expect(SessionInfo.isSet()).toBe(true)
})

it("performs one-time initialization only once", () => {
const wrapper = shallow(<App {...getProps()} />)
const app = wrapper.instance()

const oneTimeInitialization = jest.spyOn(
app,
// @ts-ignore
"handleOneTimeInitialization"
)

expect(SessionInfo.isSet()).toBe(false)

// @ts-ignore
app.handleNewReport(NEW_REPORT)
// @ts-ignore
app.handleNewReport(NEW_REPORT)
// @ts-ignore
app.handleNewReport(NEW_REPORT)

// Multiple NEW_REPORT messages should not result in one-time
// initialization being performed more than once.
expect(oneTimeInitialization).toHaveBeenCalledTimes(1)
expect(SessionInfo.isSet()).toBe(true)
})

it("throws an error if SessionInfo changes", () => {
const wrapper = shallow(<App {...getProps()} />)
const app = wrapper.instance()

expect(SessionInfo.isSet()).toBe(false)

// @ts-ignore
app.handleNewReport(NEW_REPORT)

const modified = NewReport.toObject(NEW_REPORT)
modified.initialize.userInfo.installationId = "modified"

const modifiedMessage = NewReport.fromObject(modified)

// @ts-ignore
expect(() => app.handleNewReport(modifiedMessage)).toThrow(
"Received mismatched SessionInfo"
)
})
})