Skip to content

Commit

Permalink
FIX & ADD more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Oct 22, 2019
1 parent 8dfb0c0 commit 70a77cc
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
9 changes: 7 additions & 2 deletions lib/api/src/modules/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { VERSIONCHECK } from 'global';
import semver from 'semver';
import memoize from 'memoizerific';

import { version } from 'punycode';
import { version as currentVersion } from '../version';

import { Module, API } from '../index';
Expand Down Expand Up @@ -84,7 +83,13 @@ export default function({ store, mode }: Module) {
if (!latest.version) {
return true;
}
return semver.gt(latest.version, current.version);
if (!current.version) {
return true;
}

const diff = semver.diff(current.version, latest.version);

return semver.gt(latest.version, current.version) && diff !== 'patch';
}
return false;
},
Expand Down
82 changes: 74 additions & 8 deletions lib/api/src/tests/versions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ jest.mock('@storybook/client-logger');
function createMockStore() {
let state = {
versions: {
latest: {},
current: {},
latest: {
version: '3.0.0',
},
current: {
version: '3.0.0',
},
},
};
return {
Expand Down Expand Up @@ -165,13 +169,75 @@ describe('versions API', () => {
});
});

it('versionUpdateAvailable works', async () => {
const store = createMockStore();
const { api, init, state: initialState } = initVersions({ store });
store.setState(initialState);
describe('versionUpdateAvailable', () => {
it('matching version', async () => {
const store = createMockStore();
const { api, init, state: initialState } = initVersions({ store });
store.setState({
...initialState,
versions: {
...initialState.versions,
current: { version: '5.2.1' },
latest: { version: '5.2.1' },
},
});

await init({ api: { ...api, addNotification: jest.fn() } });
await init({ api: { ...api, addNotification: jest.fn() } });

expect(api.versionUpdateAvailable()).toEqual(false);
});

it('new patch version', async () => {
const store = createMockStore();
const { api, init, state: initialState } = initVersions({ store });
store.setState({
...initialState,
versions: {
...initialState.versions,
current: { version: '5.2.1' },
latest: { version: '5.2.2' },
},
});

await init({ api: { ...api, addNotification: jest.fn() } });

expect(api.versionUpdateAvailable()).toEqual(true);
expect(api.versionUpdateAvailable()).toEqual(false);
});

it('new minor version', async () => {
const store = createMockStore();
const { api, init, state: initialState } = initVersions({ store });

await init({ api: { ...api, addNotification: jest.fn() } });

store.setState({
...initialState,
versions: {
...initialState.versions,
current: { version: '5.2.1' },
latest: { version: '5.3.1' },
},
});

expect(api.versionUpdateAvailable()).toEqual(true);
});

it('new major version', async () => {
const store = createMockStore();
const { api, init, state: initialState } = initVersions({ store });

await init({ api: { ...api, addNotification: jest.fn() } });

store.setState({
...initialState,
versions: {
...initialState.versions,
current: { version: '5.2.1' },
latest: { version: '6.2.1' },
},
});

expect(api.versionUpdateAvailable()).toEqual(true);
});
});
});

0 comments on commit 70a77cc

Please sign in to comment.