/
Notepad.ts
208 lines (194 loc) · 5.47 KB
/
Notepad.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { TinyType } from 'tiny-types';
import { LogicError } from '../../errors';
import { d } from '../../io';
import { NotepadAdapter } from './NotepadAdapter';
/**
* Stores notes recorded by an {@apilink Actor}.
*
* See {@apilink TakeNotes} and [notes](/api/core/function/notes) for more usage examples.
*
* ## Sharing a notepad between actors
*
* ```ts
* import { Actor, Cast, Notepad, TakeNotes } from '@serenity-js/core'
*
* interface MyNotes {
* auth: {
* username: string;
* password: string;
* }
* }
*
* export class Actors implements Cast {
*
* // initialise a shared notepad when the Actors class is initialised
* private readonly sharedNotepad = Notepad.with<MyNotes>({
* auth: {
* username: 'test-user',
* password: 'SuperSecretP@ssword!',
* }
* });
*
* prepare(actor: Actor): Actor {
* switch (actor.name) {
* case 'Alice':
* case 'Bob':
* // Alice and Bob will share their notepad
* return actor.whoCan(TakeNotes.using(this.sharedNotepad));
* default:
* // other actors will get their own notepads
* return actor.whoCan(TakeNotes.using(Notepad.empty<AuthCredentials>()));
* }
* }
* }
* ```
*
* ## Learn more
*
* - {@apilink TakeNotes}
* - [notes](/api/core/function/notes)
* - {@apilink Cast}
*
* @group Notes
*/
export class Notepad<Notes extends Record<any, any>> extends TinyType {
/**
* Instantiates a new empty Notepad.
*/
static empty<N extends Record<any, any>>(): Notepad<N> {
return new Notepad<N>({} as N);
}
/**
* Instantiates a new Notepad with an initial state.
*
* ```ts
* import { actorCalled, Notepad, notes, TakeNotes } from '@serenity-js/core'
* import { Ensure, equals } from '@serenity-js/assertions'
*
* interface MyNotes {
* personalDetails: {
* firstName: string;
* lastName: string;
* }
* }
*
* actorCalled('Leonard')
* .whoCan(
* TakeNotes.using(
* Notepad.with<MyNotes>({
* personalDetails: {
* firstName: 'Leonard',
* lastName: 'McLaud',
* }
* })
* )
* )
* .attemptsTo(
* Ensure.that(
* notes<MyNotes>().get('personalDetails').firstName,
* equals('Leonard')
* ),
* )
* ```
*
* @param notes
*/
static with<N extends Record<any, any>>(notes: N): Notepad<N> {
return new Notepad<N>(notes);
}
/**
* Creates a {@apilink QuestionAdapter} that simplifies access to the notes
* stored in this notepad. Allows the {@apilink Actor} to record, read, and remove notes.
*
* #### Learn more
* - [notes](/api/core/function/notes)
* - {@apilink TakeNotes}
* - {@apilink Notepad}
*/
static notes<N extends Record<any, any>>(): NotepadAdapter<N> {
return new NotepadAdapter<N>();
}
/**
* Instantiates a {@apilink Notepad} with an initial state.
*
* @param recordedNotes
* Initial state of the notepad
*/
protected constructor(private readonly recordedNotes: Notes) {
super();
}
/**
* Checks if a note identified by `subject` exists in the notepad.
*
* @param subject
* A subject (name) that uniquely identifies a given note
*
* @returns
* `true` if the note exists, `false` otherwise
*/
has<Subject extends keyof Notes>(subject: Subject): boolean {
return Object.prototype.hasOwnProperty.call(this.recordedNotes, subject);
}
/**
* Retrieves a note, identified by `subject`, from the notepad.
*
* @param subject
* A subject (name) that uniquely identifies a given note
*
* @returns
* The value of the previously recorded note.
*
* @throws {@apilink LogicError}
* Throws a {@apilink LogicError} if the note with a given `subject`
* has never been recorded.
*/
get<Subject extends keyof Notes>(subject: Subject): Notes[Subject] {
if (! this.has(subject)) {
throw new LogicError(d`Note of ${ subject } cannot be retrieved because it's never been recorded`);
}
return this.recordedNotes[subject];
}
/**
* Stores a given `value` uniquely identified by `subject` in the notepad.
*
* @param subject
* A subject (name) that uniquely identifies a given note
*
* @param value
* The value to record.
*/
set<Subject extends keyof Notes>(subject: Subject, value: Notes[Subject]): Notepad<Notes> {
this.recordedNotes[subject] = value;
return this;
}
/**
* Removes the note identified by `subject` from the notepad.
*
* @param subject
*
* @returns
* `true` if the item in the Notepad object existed and has been removed,
* `false` otherwise.
*/
delete<Subject extends keyof Notes>(subject: Subject): boolean {
if (this.has(subject)) {
return delete this.recordedNotes[subject];
}
return false;
}
/**
* Deletes all the notes stored in this notepad.
*/
clear(): void {
const keys = Object.keys(this.recordedNotes);
for (const key of keys) {
this.delete(key);
}
}
/**
* Returns the number of notes stored in the notepad.
*/
size(): number {
return Object.keys(this.recordedNotes).length;
}
}