|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { ProjectState } from './ProjectState'; |
| 18 | + |
| 19 | +describe('ProjectState', () => { |
| 20 | + describe('from', () => { |
| 21 | + it('returns ProjectState.CREATED for "created"', () => { |
| 22 | + expect(ProjectState.from('created')).toBe(ProjectState.CREATED); |
| 23 | + }); |
| 24 | + |
| 25 | + it('returns ProjectState.INITIALIZING for "initializing"', () => { |
| 26 | + expect(ProjectState.from('initializing')).toBe(ProjectState.INITIALIZING); |
| 27 | + }); |
| 28 | + |
| 29 | + it('returns ProjectState.INITIALIZED for "initialized"', () => { |
| 30 | + expect(ProjectState.from('initialized')).toBe(ProjectState.INITIALIZED); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns ProjectState.IN_PROGRESS for "inProgress"', () => { |
| 34 | + expect(ProjectState.from('inProgress')).toBe(ProjectState.IN_PROGRESS); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns ProjectState.FAILED for "failed"', () => { |
| 38 | + expect(ProjectState.from('failed')).toBe(ProjectState.FAILED); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns ProjectState.COMPLETED for "completed"', () => { |
| 42 | + expect(ProjectState.from('completed')).toBe(ProjectState.COMPLETED); |
| 43 | + }); |
| 44 | + |
| 45 | + it('throws for an invalid state', () => { |
| 46 | + expect(() => ProjectState.from('invalid')).toThrow( |
| 47 | + 'Invalid project state: "invalid". Valid: created, initializing, initialized, inProgress, failed, completed', |
| 48 | + ); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('all', () => { |
| 53 | + it('returns 6 states in defined order', () => { |
| 54 | + const all = ProjectState.all(); |
| 55 | + expect(all).toHaveLength(6); |
| 56 | + expect(all).toEqual([ |
| 57 | + ProjectState.CREATED, |
| 58 | + ProjectState.INITIALIZING, |
| 59 | + ProjectState.INITIALIZED, |
| 60 | + ProjectState.IN_PROGRESS, |
| 61 | + ProjectState.FAILED, |
| 62 | + ProjectState.COMPLETED, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('values', () => { |
| 68 | + it('returns raw string values for all states', () => { |
| 69 | + expect(ProjectState.values()).toEqual([ |
| 70 | + 'created', |
| 71 | + 'initializing', |
| 72 | + 'initialized', |
| 73 | + 'inProgress', |
| 74 | + 'failed', |
| 75 | + 'completed', |
| 76 | + ]); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('individual predicates', () => { |
| 81 | + it('isCreated', () => { |
| 82 | + expect(ProjectState.CREATED.isCreated()).toBe(true); |
| 83 | + expect(ProjectState.INITIALIZING.isCreated()).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('isInitializing', () => { |
| 87 | + expect(ProjectState.INITIALIZING.isInitializing()).toBe(true); |
| 88 | + expect(ProjectState.CREATED.isInitializing()).toBe(false); |
| 89 | + }); |
| 90 | + |
| 91 | + it('isInitialized', () => { |
| 92 | + expect(ProjectState.INITIALIZED.isInitialized()).toBe(true); |
| 93 | + expect(ProjectState.CREATED.isInitialized()).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('isInProgress', () => { |
| 97 | + expect(ProjectState.IN_PROGRESS.isInProgress()).toBe(true); |
| 98 | + expect(ProjectState.CREATED.isInProgress()).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('isFailed', () => { |
| 102 | + expect(ProjectState.FAILED.isFailed()).toBe(true); |
| 103 | + expect(ProjectState.CREATED.isFailed()).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('isComplete', () => { |
| 107 | + expect(ProjectState.COMPLETED.isComplete()).toBe(true); |
| 108 | + expect(ProjectState.CREATED.isComplete()).toBe(false); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('ordinal', () => { |
| 113 | + it('assigns sequential ordinals from 0 to 5', () => { |
| 114 | + expect(ProjectState.CREATED.ordinal).toBe(0); |
| 115 | + expect(ProjectState.INITIALIZING.ordinal).toBe(1); |
| 116 | + expect(ProjectState.INITIALIZED.ordinal).toBe(2); |
| 117 | + expect(ProjectState.IN_PROGRESS.ordinal).toBe(3); |
| 118 | + expect(ProjectState.FAILED.ordinal).toBe(4); |
| 119 | + expect(ProjectState.COMPLETED.ordinal).toBe(5); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('toString', () => { |
| 124 | + it('returns the raw string value', () => { |
| 125 | + expect(ProjectState.CREATED.toString()).toBe('created'); |
| 126 | + expect(ProjectState.INITIALIZING.toString()).toBe('initializing'); |
| 127 | + expect(ProjectState.INITIALIZED.toString()).toBe('initialized'); |
| 128 | + expect(ProjectState.IN_PROGRESS.toString()).toBe('inProgress'); |
| 129 | + expect(ProjectState.FAILED.toString()).toBe('failed'); |
| 130 | + expect(ProjectState.COMPLETED.toString()).toBe('completed'); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('equals', () => { |
| 135 | + it('returns true for same instance', () => { |
| 136 | + expect(ProjectState.CREATED.equals(ProjectState.CREATED)).toBe(true); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns true for ProjectState.from result (flyweight identity)', () => { |
| 140 | + expect(ProjectState.CREATED.equals(ProjectState.from('created'))).toBe( |
| 141 | + true, |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('returns false for different states', () => { |
| 146 | + expect(ProjectState.CREATED.equals(ProjectState.FAILED)).toBe(false); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('flyweight identity', () => { |
| 151 | + it('from() returns the exact same instance', () => { |
| 152 | + expect(ProjectState.from('created')).toBe(ProjectState.CREATED); |
| 153 | + expect(ProjectState.from('initializing')).toBe(ProjectState.INITIALIZING); |
| 154 | + expect(ProjectState.from('initialized')).toBe(ProjectState.INITIALIZED); |
| 155 | + expect(ProjectState.from('inProgress')).toBe(ProjectState.IN_PROGRESS); |
| 156 | + expect(ProjectState.from('failed')).toBe(ProjectState.FAILED); |
| 157 | + expect(ProjectState.from('completed')).toBe(ProjectState.COMPLETED); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments