Skip to content

Commit

Permalink
os update button fix part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Jan 4, 2019
1 parent 2f6123d commit f2571ad
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
7 changes: 4 additions & 3 deletions webpack/auth/__tests__/actions_test.ts
Expand Up @@ -22,14 +22,15 @@ jest.mock("../../api/api", () => ({

jest.mock("../../devices/actions", () => ({
fetchReleases: jest.fn(),
fetchLatestGHBetaRelease: jest.fn(),
fetchMinOsFeatureData: jest.fn(),
}));

import { didLogin } from "../actions";
import { Actions } from "../../constants";
import { API } from "../../api/api";
import { AuthState } from "../interfaces";
import { fetchReleases } from "../../devices/actions";
import { fetchReleases, fetchLatestGHBetaRelease } from "../../devices/actions";

const mockToken = (): AuthState => ({
token: {
Expand All @@ -56,7 +57,7 @@ describe("didLogin()", () => {
mockAuth.token.unencoded.beta_os_update_server = "beta_os_update_server";
didLogin(mockAuth, dispatch);
expect(fetchReleases).toHaveBeenCalledWith("os_update_server");
expect(fetchReleases).toHaveBeenCalledWith("beta_os_update_server",
{ beta: true });
expect(fetchLatestGHBetaRelease)
.toHaveBeenCalledWith("beta_os_update_server");
});
});
5 changes: 3 additions & 2 deletions webpack/auth/actions.ts
@@ -1,6 +1,7 @@
import axios from "axios";
import {
fetchReleases, fetchMinOsFeatureData, FEATURE_MIN_VERSIONS_URL
fetchReleases, fetchMinOsFeatureData, FEATURE_MIN_VERSIONS_URL,
fetchLatestGHBetaRelease
} from "../devices/actions";
import { AuthState } from "./interfaces";
import { ReduxAction } from "../redux/interfaces";
Expand All @@ -20,7 +21,7 @@ export function didLogin(authState: AuthState, dispatch: Function) {
const { os_update_server, beta_os_update_server } = authState.token.unencoded;
dispatch(fetchReleases(os_update_server));
beta_os_update_server && beta_os_update_server != "NOT_SET" &&
dispatch(fetchReleases(beta_os_update_server, { beta: true }));
dispatch(fetchLatestGHBetaRelease(beta_os_update_server));
dispatch(getFirstPartyFarmwareList());
dispatch(fetchMinOsFeatureData(FEATURE_MIN_VERSIONS_URL));
dispatch(setToken(authState));
Expand Down
26 changes: 26 additions & 0 deletions webpack/devices/__tests__/actions_test.ts
Expand Up @@ -410,6 +410,32 @@ describe("fetchReleases()", () => {
});
});

describe("fetchLatestGHBetaRelease()", () => {
it("fetches latest beta OS release version", async () => {
mockGetRelease = Promise.resolve({ data: [{ tag_name: "v1.0.0-beta" }] });
const dispatch = jest.fn();
await actions.fetchLatestGHBetaRelease("url/001")(dispatch);
expect(axios.get).toHaveBeenCalledWith("url");
expect(error).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
payload: { version: "1.0.0-beta", commit: undefined },
type: Actions.FETCH_BETA_OS_UPDATE_INFO_OK
});
});

it("fails to fetches latest beta OS release version", async () => {
mockGetRelease = Promise.reject("error");
const dispatch = jest.fn();
await actions.fetchLatestGHBetaRelease("url/001")(dispatch);
await expect(axios.get).toHaveBeenCalledWith("url");
expect(error).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledWith({
payload: "error",
type: Actions.FETCH_BETA_OS_UPDATE_INFO_ERROR
});
});
});

describe("fetchMinOsFeatureData()", () => {
afterEach(() =>
jest.restoreAllMocks());
Expand Down
33 changes: 32 additions & 1 deletion webpack/devices/actions.ts
Expand Up @@ -151,6 +151,37 @@ export function requestDiagnostic() {
return getDevice().dumpInfo().then(commandOK(noun), commandErr(noun));
}

const tagNameToVersionString = (tagName: string): string =>
tagName.toLowerCase().replace("v", "");

/**
* Fetch FarmBot OS beta release data.
* Ignores a specific release provided by the url (i.e., `releases/1234`)
* in favor of the latest `-beta` release.
*/
export const fetchLatestGHBetaRelease =
(url: string) =>
(dispatch: Function) => {
const urlArray = url.split("/");
const releasesURL = urlArray.splice(0, urlArray.length - 1).join("/");
axios
.get<GithubRelease[]>(releasesURL)
.then(resp => {
const latestBeta = resp.data
.filter(x => x.tag_name.includes("beta"))[0];
const { tag_name, target_commitish } = latestBeta;
const version = tagNameToVersionString(tag_name);
dispatch({
type: Actions.FETCH_BETA_OS_UPDATE_INFO_OK,
payload: { version, commit: target_commitish }
});
})
.catch(ferror => dispatch({
type: "FETCH_BETA_OS_UPDATE_INFO_ERROR",
payload: ferror
}));
};

/** Fetch FarmBot OS release data. */
export const fetchReleases =
(url: string, options = { beta: false }) =>
Expand All @@ -159,7 +190,7 @@ export const fetchReleases =
.get<GithubRelease>(url)
.then(resp => {
const { tag_name, target_commitish } = resp.data;
const version = tag_name.toLowerCase().replace("v", "");
const version = tagNameToVersionString(tag_name);
dispatch({
type: options.beta
? Actions.FETCH_BETA_OS_UPDATE_INFO_OK
Expand Down

0 comments on commit f2571ad

Please sign in to comment.