Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Jul 22, 2020
1 parent d3f8bdd commit 7706ee0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 20 deletions.
29 changes: 12 additions & 17 deletions src/nextVersion.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import { VersionUpgrade } from './getVersionUpgrade';
import { minVersionBump } from './minVersionBump';
import { TokenInfo, TokenList, Version } from './types';
import { Version } from './types';

/**
* Returns the next version of the list given a base list and the updated token list.
* @param base base list
* @param updatedList updated list of tokens for the next list
* Returns the next version of the list given a base version and the upgrade type
* @param base current version
* @param bump the upgrade type
*/
export function nextVersion(
base: TokenList,
updatedList: TokenInfo[]
): Version {
const bump = minVersionBump(base.tokens, updatedList);
export function nextVersion(base: Version, bump: VersionUpgrade): Version {
switch (bump) {
case VersionUpgrade.NONE:
return base.version;
return base;

case VersionUpgrade.MAJOR:
return { major: base.version.major + 1, minor: 0, patch: 0 };
return { major: base.major + 1, minor: 0, patch: 0 };

case VersionUpgrade.MINOR:
return {
major: base.version.major,
minor: base.version.minor + 1,
major: base.major,
minor: base.minor + 1,
patch: 0,
};

case VersionUpgrade.PATCH:
return {
major: base.version.major,
minor: base.version.minor,
patch: base.version.patch + 1,
major: base.major,
minor: base.minor,
patch: base.patch + 1,
};
}
}
37 changes: 34 additions & 3 deletions test/diffTokenLists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ const tokenA: TokenInfo = {
decimals: 18,
tags: ['hello', 'world'],
};
const tokenAChanged: TokenInfo = {
const tokenAChangedNameDecimals: TokenInfo = {
...tokenA,
name: 'blah',
decimals: 12,
};
const tokenAChangedTags: TokenInfo = {
...tokenA,
tags: ['hello', 'worlds'],
};
const tokenB: TokenInfo = {
chainId: 1,
address: '0x0b',
logoURI: 'ipfs://blah',
symbol: 'defg',
name: 'token b',
decimals: 9,
tags: ['hello', 'world'],
tags: ['token', 'other'],
};

describe('#diffTokenLists', () => {
Expand All @@ -34,7 +38,9 @@ describe('#diffTokenLists', () => {
});

it('change name', () => {
expect(diffTokenLists([tokenB, tokenA], [tokenB, tokenAChanged])).toEqual({
expect(
diffTokenLists([tokenB, tokenA], [tokenB, tokenAChangedNameDecimals])
).toEqual({
added: [],
removed: [],
changed: {
Expand All @@ -44,4 +50,29 @@ describe('#diffTokenLists', () => {
},
});
});

it('change tags', () => {
expect(diffTokenLists([tokenB, tokenA], [tokenAChangedTags])).toEqual({
added: [],
removed: [tokenB],
changed: {
1: {
'0x0a': ['tags'],
},
},
});
});
it('remove tags', () => {
expect(
diffTokenLists([tokenB, tokenA], [{ ...tokenA, tags: undefined }])
).toEqual({
added: [],
removed: [tokenB],
changed: {
1: {
'0x0a': ['tags'],
},
},
});
});
});
52 changes: 52 additions & 0 deletions test/minVersionBump.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { minVersionBump, TokenInfo, VersionUpgrade } from '../src';
const tokenA: TokenInfo = {
chainId: 1,
address: '0x0a',
logoURI: 'ipfs://test',
symbol: 'abcd',
name: 'token a',
decimals: 18,
tags: ['hello', 'world'],
};
const tokenAChangedNameDecimals: TokenInfo = {
...tokenA,
name: 'blah',
decimals: 12,
};
const tokenAChangedTags: TokenInfo = {
...tokenA,
tags: ['hello', 'worlds'],
};
const tokenB: TokenInfo = {
chainId: 1,
address: '0x0b',
logoURI: 'ipfs://blah',
symbol: 'defg',
name: 'token b',
decimals: 9,
tags: ['token', 'other'],
};
describe('#minVersionBump', () => {
it('empty', () => {
expect(minVersionBump([], [])).toBe(VersionUpgrade.NONE);
});
it('patch for tag changes only', () => {
expect(minVersionBump([tokenA], [tokenAChangedTags])).toBe(
VersionUpgrade.PATCH
);
});
it('patch for name/decimals changes', () => {
expect(minVersionBump([tokenA], [tokenAChangedNameDecimals])).toBe(
VersionUpgrade.PATCH
);
});
it('minor for remove only', () => {
expect(minVersionBump([tokenA], [])).toBe(VersionUpgrade.MINOR);
});
it('major for add', () => {
expect(minVersionBump([], [tokenA])).toBe(VersionUpgrade.MAJOR);
});
it('major for add/remove', () => {
expect(minVersionBump([tokenB], [tokenA])).toBe(VersionUpgrade.MAJOR);
});
});
48 changes: 48 additions & 0 deletions test/nextVersion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { nextVersion, VersionUpgrade } from '../src';

describe('#nextVersion', () => {
it('none', () => {
expect(
nextVersion({ major: 1, minor: 0, patch: 0 }, VersionUpgrade.NONE)
).toEqual({ major: 1, minor: 0, patch: 0 });
expect(
nextVersion({ major: 1, minor: 2, patch: 0 }, VersionUpgrade.NONE)
).toEqual({ major: 1, minor: 2, patch: 0 });
expect(
nextVersion({ major: 2, minor: 5, patch: 3 }, VersionUpgrade.NONE)
).toEqual({ major: 2, minor: 5, patch: 3 });
});
it('patch', () => {
expect(
nextVersion({ major: 1, minor: 0, patch: 0 }, VersionUpgrade.PATCH)
).toEqual({ major: 1, minor: 0, patch: 1 });
expect(
nextVersion({ major: 1, minor: 2, patch: 0 }, VersionUpgrade.PATCH)
).toEqual({ major: 1, minor: 2, patch: 1 });
expect(
nextVersion({ major: 2, minor: 5, patch: 3 }, VersionUpgrade.PATCH)
).toEqual({ major: 2, minor: 5, patch: 4 });
});
it('minor', () => {
expect(
nextVersion({ major: 1, minor: 0, patch: 0 }, VersionUpgrade.MINOR)
).toEqual({ major: 1, minor: 1, patch: 0 });
expect(
nextVersion({ major: 1, minor: 2, patch: 0 }, VersionUpgrade.MINOR)
).toEqual({ major: 1, minor: 3, patch: 0 });
expect(
nextVersion({ major: 2, minor: 5, patch: 3 }, VersionUpgrade.MINOR)
).toEqual({ major: 2, minor: 6, patch: 0 });
});
it('major', () => {
expect(
nextVersion({ major: 1, minor: 0, patch: 0 }, VersionUpgrade.MAJOR)
).toEqual({ major: 2, minor: 0, patch: 0 });
expect(
nextVersion({ major: 1, minor: 2, patch: 0 }, VersionUpgrade.MAJOR)
).toEqual({ major: 2, minor: 0, patch: 0 });
expect(
nextVersion({ major: 2, minor: 5, patch: 3 }, VersionUpgrade.MAJOR)
).toEqual({ major: 3, minor: 0, patch: 0 });
});
});

0 comments on commit 7706ee0

Please sign in to comment.