Skip to content

Commit f46fdbf

Browse files
committed
Add image-tag model
1 parent 694fb0b commit f46fdbf

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/model/image-tag.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { trimStart } from 'lodash-es';
2+
3+
class ImageTag {
4+
constructor({ version }) {
5+
if (!ImageTag.versionPattern.test(version)) {
6+
throw new Error(`Invalid version "${version}".`);
7+
}
8+
9+
this.repository = 'gableroux';
10+
this.name = 'unity3d';
11+
this.version = version;
12+
}
13+
14+
static get versionPattern() {
15+
return /^20\d{2}\.\d\.\w{3,4}|3$/;
16+
}
17+
18+
get tag() {
19+
return this.version;
20+
}
21+
22+
get image() {
23+
return trimStart(`${this.repository}/${this.name}`, '/');
24+
}
25+
26+
toString() {
27+
return `${this.image}:${this.tag}`;
28+
}
29+
}
30+
31+
export default ImageTag;

src/model/image-tag.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import ImageTag from './image-tag';
2+
3+
describe('UnityImageVersion', () => {
4+
describe('constructor', () => {
5+
const some = {
6+
version: '2020.0.00f0',
7+
};
8+
9+
it('can be called', () => {
10+
expect(() => new ImageTag(some)).not.toThrow();
11+
});
12+
13+
it('accepts parameters and sets the right properties', () => {
14+
const image = new ImageTag(some);
15+
16+
expect(image.repository).toStrictEqual('gableroux');
17+
expect(image.name).toStrictEqual('unity3d');
18+
expect(image.version).toStrictEqual(some.version);
19+
});
20+
21+
test.each(['2000.0.0f0', '2011.1.11f1'])('accepts %p version format', version => {
22+
expect(() => new ImageTag({ version })).not.toThrow();
23+
});
24+
25+
test.each(['some version', '', 1, null])('throws for incorrect versions %p', version => {
26+
expect(() => new ImageTag({ version })).toThrow();
27+
});
28+
});
29+
30+
describe('toString', () => {
31+
it('returns the correct version', () => {
32+
const image = new ImageTag({ version: '2099.1.1111' });
33+
34+
expect(image.toString()).toStrictEqual(`gableroux/unity3d:2099.1.1111`);
35+
});
36+
});
37+
});

0 commit comments

Comments
 (0)