Skip to content

Commit

Permalink
update os release notes fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Oct 30, 2019
1 parent 71bb473 commit e57d476
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
41 changes: 34 additions & 7 deletions frontend/devices/components/__tests__/farmbot_os_settings_test.tsx
@@ -1,6 +1,6 @@
let mockReleaseNoteData = {};
let mockReleaseNoteResponse = Promise.resolve({ data: "" });
jest.mock("axios", () => ({
get: jest.fn(() => Promise.resolve(mockReleaseNoteData))
get: jest.fn(() => mockReleaseNoteResponse)
}));

jest.mock("../../../api/crud", () => ({
Expand Down Expand Up @@ -53,24 +53,44 @@ describe("<FarmbotOsSettings />", () => {
});

it("fetches OS release notes", async () => {
mockReleaseNoteData = { data: "intro\n\n# v6\n\n* note" };
mockReleaseNoteResponse = Promise.resolve({
data: "intro\n\n# v6\n\n* note"
});
const osSettings = await mount<FarmbotOsSettings>(<FarmbotOsSettings
{...fakeProps()} />);
await expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining("RELEASE_NOTES.md"));
expect(osSettings.instance().state.osReleaseNotesHeading)
expect(osSettings.instance().osReleaseNotes.heading)
.toEqual("FarmBot OS v6");
expect(osSettings.instance().state.osReleaseNotes)
expect(osSettings.instance().osReleaseNotes.notes)
.toEqual("* note");
});

it("doesn't fetch OS release notes", async () => {
mockReleaseNoteData = { data: "empty notes" };
mockReleaseNoteResponse = Promise.resolve({ data: "" });
const osSettings = await mount<FarmbotOsSettings>(<FarmbotOsSettings
{...fakeProps()} />);
await expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining("RELEASE_NOTES.md"));
expect(osSettings.instance().state.osReleaseNotes)
expect(osSettings.instance().state.allOsReleaseNotes)
.toEqual("");
expect(osSettings.instance().osReleaseNotes.heading)
.toEqual("FarmBot OS v6");
expect(osSettings.instance().osReleaseNotes.notes)
.toEqual("Could not get release notes.");
});

it("errors while fetching OS release notes", async () => {
mockReleaseNoteResponse = Promise.reject({ error: "" });
const osSettings = await mount<FarmbotOsSettings>(<FarmbotOsSettings
{...fakeProps()} />);
await expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining("RELEASE_NOTES.md"));
expect(osSettings.instance().state.allOsReleaseNotes)
.toEqual("");
expect(osSettings.instance().osReleaseNotes.heading)
.toEqual("FarmBot OS v6");
expect(osSettings.instance().osReleaseNotes.notes)
.toEqual("Could not get release notes.");
});

Expand All @@ -82,4 +102,11 @@ describe("<FarmbotOsSettings />", () => {
.simulate("change", { currentTarget: { value: newName } });
expect(edit).toHaveBeenCalledWith(p.deviceAccount, { name: newName });
});

it("displays boot sequence selector", () => {
const p = fakeProps();
p.shouldDisplay = () => true;
const osSettings = shallow(<FarmbotOsSettings {...p} />);
expect(osSettings.find("BootSequenceSelector").length).toEqual(1);
});
});
38 changes: 21 additions & 17 deletions frontend/devices/components/farmbot_os_settings.tsx
Expand Up @@ -29,27 +29,31 @@ const OS_RELEASE_NOTES_URL =

export class FarmbotOsSettings
extends React.Component<FarmbotOsProps, FarmbotOsState> {
state = { osReleaseNotesHeading: "", osReleaseNotes: "" };
state: FarmbotOsState = { allOsReleaseNotes: "" };

componentDidMount() {
this.fetchReleaseNotes(OS_RELEASE_NOTES_URL,
(this.props.bot.hardware.informational_settings
.controller_version || "6").split(".")[0]);
this.fetchReleaseNotes(OS_RELEASE_NOTES_URL);
}

fetchReleaseNotes = (url: string, osMajorVersion: string) => {
get osMajorVersion() {
return (this.props.bot.hardware.informational_settings
.controller_version || "6").split(".")[0];
}

fetchReleaseNotes = (url: string) => {
axios
.get<string>(url)
.then(resp => {
const osReleaseNotes = resp.data
.split("# v")
.filter(x => x.startsWith(osMajorVersion))[0]
.split("\n\n").slice(1).join("\n");
const osReleaseNotesHeading = "FarmBot OS v" + osMajorVersion;
this.setState({ osReleaseNotesHeading, osReleaseNotes });
})
.catch(() =>
this.setState({ osReleaseNotes: "Could not get release notes." }));
.then(resp => this.setState({ allOsReleaseNotes: resp.data }))
.catch(() => this.setState({ allOsReleaseNotes: "" }));
}

get osReleaseNotes() {
const notes = (this.state.allOsReleaseNotes
.split("# v")
.filter(x => x.startsWith(this.osMajorVersion))[0] || "")
.split("\n\n").slice(1).join("\n") || t("Could not get release notes.");
const heading = "FarmBot OS v" + this.osMajorVersion;
return { heading, notes };
}

changeBot = (e: React.FormEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -119,8 +123,8 @@ export class FarmbotOsSettings
|| this.props.isValidFbosConfig}>
<FarmbotOsRow
bot={this.props.bot}
osReleaseNotesHeading={this.state.osReleaseNotesHeading}
osReleaseNotes={this.state.osReleaseNotes}
osReleaseNotesHeading={this.osReleaseNotes.heading}
osReleaseNotes={this.osReleaseNotes.notes}
dispatch={this.props.dispatch}
sourceFbosConfig={sourceFbosConfig}
shouldDisplay={this.props.shouldDisplay}
Expand Down
3 changes: 1 addition & 2 deletions frontend/devices/interfaces.ts
Expand Up @@ -170,8 +170,7 @@ export interface FarmbotOsProps {
}

export interface FarmbotOsState {
osReleaseNotesHeading: string;
osReleaseNotes: string;
allOsReleaseNotes: string;
}

export interface McuInputBoxProps {
Expand Down

0 comments on commit e57d476

Please sign in to comment.