-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated version comparison to only notify for older versions
- Loading branch information
Showing
4 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import axios from 'axios' | ||
import MockAdapter from 'axios-mock-adapter' | ||
|
||
import { checkVersion } from '../../app/modules/metadata' | ||
import * as notifications from '../../app/modules/notifications' | ||
import { TEST_NETWORK_ID } from '../../app/core/networks' | ||
import { version } from '../../package.json' | ||
|
||
const axiosMock = new MockAdapter(axios) | ||
|
||
describe('metadata module tests', () => { | ||
describe('checkVersion tests', () => { | ||
const dispatch = jest.fn() | ||
const getState = () => ({ api: { NETWORK: TEST_NETWORK_ID } }) | ||
|
||
const generateNewerVersion = (version) => { | ||
const parts = version.split('.') | ||
const last = parts.pop() | ||
return [...parts, parseInt(last) + 1].join('.') | ||
} | ||
|
||
test('it does not show a warning when the versions match', async (done) => { | ||
const spy = jest.spyOn(notifications, 'showWarningNotification') | ||
|
||
axiosMock | ||
.onGet('http://testnet-api.wallet.cityofzion.io/v2/version') | ||
.reply(200, { version }) | ||
|
||
await checkVersion()(dispatch, getState) | ||
expect(spy).not.toHaveBeenCalled() | ||
|
||
axiosMock.restore() | ||
done() | ||
}) | ||
|
||
test.only("it shows a wraning when the versions don't match", async (done) => { | ||
const spy = jest.spyOn(notifications, 'showWarningNotification') | ||
|
||
axiosMock | ||
.onGet('http://testnet-api.wallet.cityofzion.io/v2/version') | ||
.reply(200, { version: generateNewerVersion(version) }) | ||
|
||
await checkVersion()(dispatch, getState) | ||
expect(spy).toHaveBeenCalled() | ||
|
||
axiosMock.restore() | ||
done() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
// @flow | ||
import axios from 'axios' | ||
import { api } from 'neon-js' | ||
import compareVersions from 'compare-versions' | ||
|
||
import { showWarningNotification } from './notifications' | ||
import asyncWrap from '../core/asyncHelper' | ||
import { getNetwork } from '../core/deprecated' | ||
import { NEON_WALLET_RELEASE_LINK, NOTIFICATION_POSITIONS } from '../core/constants' | ||
import { version } from '../../package.json' | ||
|
||
const DOWNLOAD_LINK = `<a href='${NEON_WALLET_RELEASE_LINK}' target='_blank' class="notification-link">${NEON_WALLET_RELEASE_LINK}</a>` | ||
|
||
// Actions | ||
export const checkVersion = () => async (dispatch: DispatchType, getState: GetStateType) => { | ||
const state = getState() | ||
const net = getNetwork(state) | ||
const apiEndpoint = api.neonDB.getAPIEndpoint(net) | ||
|
||
const [err, res] = await asyncWrap(axios.get(`${apiEndpoint}/v2/version`)) | ||
const shouldUpdate = res && res.data && res.data.version !== version | ||
if (err || shouldUpdate) { | ||
const link = `<a href='${NEON_WALLET_RELEASE_LINK}' target='_blank' class="notification-link">${NEON_WALLET_RELEASE_LINK}</a>` | ||
const message = err | ||
? `Error checking wallet version! Please make sure you have downloaded the latest version: ${link}` | ||
: `Your wallet is out of date! Please download the latest version from ${link}` | ||
return dispatch( | ||
showWarningNotification({ | ||
message, | ||
autoDismiss: 0, | ||
stack: true, | ||
position: NOTIFICATION_POSITIONS.BOTTOM_CENTER | ||
}) | ||
) | ||
const showError = (message) => { | ||
return dispatch(showWarningNotification({ | ||
message, | ||
autoDismiss: 0, | ||
stack: true, | ||
position: NOTIFICATION_POSITIONS.BOTTOM_CENTER | ||
})) | ||
} | ||
|
||
try { | ||
const response = await axios.get(`${apiEndpoint}/v2/version`) | ||
const shouldUpdate = response && response.data && compareVersions(version, response.data.version) === -1 | ||
|
||
if (shouldUpdate) { | ||
showError(`Your wallet is out of date! Please download the latest version from ${DOWNLOAD_LINK}`) | ||
} | ||
} catch (err) { | ||
showError(`Error checking wallet version! Please make sure you have downloaded the latest version: ${DOWNLOAD_LINK}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters