notu 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,245 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import NoteTag from './NoteTag';
3
- import Note from './Note';
4
- import Tag from './Tag';
5
- import Attr from './Attr';
6
-
7
-
8
- test('Gets initiated as new', () => {
9
- const nt = new NoteTag();
10
- expect(nt.isNew).toBe(true);
11
- });
12
-
13
- test('Gets initiated with sensible defaults', () => {
14
- const nt = new NoteTag();
15
- expect(nt.noteId).toBe(0);
16
- expect(nt.tagId).toBe(0);
17
- });
18
-
19
- test('Can duplicate itself', () => {
20
- const nt = new NoteTag();
21
- nt.noteId = 123;
22
- nt.tagId = 234;
23
- const copy = nt.duplicate();
24
- expect(copy.noteId).toBe(nt.noteId);
25
- expect(copy.tagId).toBe(nt.tagId);
26
- });
27
-
28
-
29
- test('Set noteId marks object as dirty if currently clean', () => {
30
- const nt = new NoteTag().clean();
31
- nt.noteId = 123;
32
- expect(nt.isDirty).toBe(true);
33
- });
34
-
35
- test('Set noteId doesnt change state if new', () => {
36
- const nt = new NoteTag().new();
37
- nt.noteId = 123;
38
- expect(nt.isNew).toBe(true);
39
- });
40
-
41
- test('Set noteId doesnt change state if value not different', () => {
42
- const nt = new NoteTag().clean();
43
- nt.noteId = nt.noteId;
44
- expect(nt.isClean).toBe(true);
45
- });
46
-
47
-
48
- test('Set tagId marks object as dirty if currently clean', () => {
49
- const nt = new NoteTag().clean();
50
- nt.tagId = 123;
51
- expect(nt.isDirty).toBe(true);
52
- });
53
-
54
- test('Set tagId doesnt change state if new', () => {
55
- const nt = new NoteTag().new();
56
- nt.tagId = 123;
57
- expect(nt.isNew).toBe(true);
58
- });
59
-
60
- test('Set tagId doesnt change state if value not different', () => {
61
- const nt = new NoteTag().clean();
62
- nt.tagId = nt.tagId;
63
- expect(nt.isClean).toBe(true);
64
- });
65
-
66
-
67
- test('Setting note with id different than current noteId updates state', () => {
68
- const nt = new NoteTag();
69
- nt.noteId = 123;
70
- nt.clean();
71
- const note = new Note();
72
- note.id = 234;
73
-
74
- nt.note = note;
75
-
76
- expect(nt.noteId).toBe(234);
77
- expect(nt.isDirty).toBe(true);
78
- });
79
-
80
- test('Setting note with id same as current noteId preserves state', () => {
81
- const nt = new NoteTag();
82
- nt.noteId = 27;
83
- nt.clean();
84
- const note = new Note();
85
- note.id = 27;
86
-
87
- nt.note = note;
88
-
89
- expect(nt.noteId).toBe(27);
90
- expect(nt.isClean).toBe(true);
91
- });
92
-
93
- test('Setting noteId to new value removes note object', () => {
94
- const nt = new NoteTag();
95
- const note = new Note();
96
- note.id = 80;
97
- nt.note = note;
98
-
99
- nt.noteId = 81;
100
-
101
- expect(nt.note).toBeNull();
102
- });
103
-
104
- test('Setting noteId to same as current note id preserves it', () => {
105
- const nt = new NoteTag();
106
- const note = new Note();
107
- note.id = 80;
108
- nt.note = note;
109
-
110
- nt.noteId = 80;
111
-
112
- expect(nt.note).not.toBeNull();
113
- });
114
-
115
-
116
- test('Setting tag with id different than current tagId updates state', () => {
117
- const nt = new NoteTag();
118
- nt.tagId = 123;
119
- nt.clean();
120
- const tag = new Tag();
121
- tag.id = 234;
122
-
123
- nt.tag = tag;
124
-
125
- expect(nt.tagId).toBe(234);
126
- expect(nt.isDirty).toBe(true);
127
- });
128
-
129
- test('Setting tag with id same as current tagId preserves state', () => {
130
- const nt = new NoteTag();
131
- nt.tagId = 27;
132
- nt.clean();
133
- const tag = new Tag();
134
- tag.id = 27;
135
-
136
- nt.tag = tag;
137
-
138
- expect(nt.tagId).toBe(27);
139
- expect(nt.isClean).toBe(true);
140
- });
141
-
142
- test('Setting tagId to new value removes tag object', () => {
143
- const nt = new NoteTag();
144
- const tag = new Tag();
145
- tag.id = 80;
146
- nt.tag = tag;
147
-
148
- nt.tagId = 81;
149
-
150
- expect(nt.tag).toBeNull();
151
- });
152
-
153
- test('Setting tagId to same as current tag id preserves it', () => {
154
- const nt = new NoteTag();
155
- const tag = new Tag();
156
- tag.id = 80;
157
- nt.tag = tag;
158
-
159
- nt.tagId = 80;
160
-
161
- expect(nt.tag).not.toBeNull();
162
- });
163
-
164
-
165
- test('validate passes if noteId is 0 and state is new', () => {
166
- const nt = new NoteTag();
167
- nt.tagId = 123;
168
- expect(nt.validate()).toBe(true);
169
- });
170
-
171
- test('validate fails if noteId is 0 and state is not new', () => {
172
- const nt = new NoteTag();
173
- nt.tagId = 123;
174
- nt.clean();
175
- expect(nt.validate()).toBe(false);
176
- });
177
-
178
- test('validate fails if tagId is 0', () => {
179
- const nt = new NoteTag();
180
- nt.noteId = 123;
181
- expect(nt.validate()).toBe(false);
182
- });
183
-
184
- test('validate throws error if arg set to true', () => {
185
- const nt = new NoteTag();
186
- expect(() => nt.validate(true)).toThrowError();
187
- });
188
-
189
- test('validate fails if noteId = tagId', () => {
190
- const nt = new NoteTag();
191
- nt.noteId = 123;
192
- nt.tagId = 123;
193
- expect(nt.validate()).toBe(false);
194
- });
195
-
196
- test('validate succeeds if noteId & tagId are both positive, different values', () => {
197
- const nt = new NoteTag();
198
- nt.noteId = 123;
199
- nt.tagId = 234;
200
- expect(nt.validate()).toBe(true);
201
- });
202
-
203
-
204
- test('addAttr adds NoteAttr object to parent note', () => {
205
- const note = new Note();
206
- const tag = new Tag('hello').clean();
207
- tag.id = 123;
208
- const attr = new Attr().clean();
209
- attr.id = 234;
210
- const nt = note.addTag(tag);
211
-
212
- nt.addAttr(attr);
213
-
214
- expect(note.attrs.length).toBe(1);
215
- expect(note.attrs[0].note).toBe(note);
216
- expect(note.attrs[0].attr).toBe(attr);
217
- expect(note.attrs[0].tag).toBe(tag);
218
- });
219
-
220
- test('addAttr throws error if note property not set', () => {
221
- const nt = new NoteTag();
222
- nt.noteId = 123;
223
- nt.tag = new Tag('hello');
224
- nt.tag.id = 234;
225
- const attr = new Attr().clean();
226
- attr.id = 345;
227
-
228
- expect(() => nt.addAttr(attr)).toThrowError();
229
- });
230
-
231
- test('attrs returns NoteAttr objects with matching tagId', () => {
232
- const note = new Note();
233
- const tag = new Tag('hello').clean();
234
- tag.id = 123;
235
- const attr1 = new Attr().clean();
236
- attr1.id = 234;
237
- const attr2 = new Attr().clean();
238
- attr2.id = 345;
239
- const nt = note.addTag(tag);
240
- nt.addAttr(attr1);
241
- note.addAttr(attr2);
242
-
243
- expect(nt.attrs.length).toBe(1);
244
- expect(nt.attrs[0].attr).toBe(attr1);
245
- });
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- import Note from './Note';
4
- import ModelWithState from './ModelWithState';
5
- import Tag from './Tag';
6
- import Attr from './Attr';
7
- import NoteAttr from './NoteAttr';
8
-
9
-
10
- export default class NoteTag extends ModelWithState<NoteTag> {
11
- private _noteId: number = 0;
12
- get noteId(): number { return this._noteId; }
13
- set noteId(value: number) {
14
- if (value !== this._noteId) {
15
- this._noteId = value;
16
- if (value !== this.note?.id ?? 0)
17
- this._note = null;
18
- if (this.isClean)
19
- this.dirty();
20
- }
21
- }
22
-
23
- private _note: Note = null;
24
- get note(): Note { return this._note; }
25
- set note(value: Note) {
26
- this._note = value;
27
- this.noteId = value?.id ?? 0;
28
- }
29
-
30
-
31
- private _tagId: number = 0;
32
- get tagId(): number { return this._tagId; }
33
- set tagId(value: number) {
34
- if (value !== this._tagId) {
35
- this._tagId = value;
36
- if (value !== this.tag?.id ?? 0)
37
- this._tag = null;
38
- if (this.isClean)
39
- this.dirty();
40
- }
41
- }
42
-
43
- private _tag: Tag = null;
44
- get tag(): Tag { return this._tag; }
45
- set tag(value: Tag) {
46
- this._tag = value;
47
- this.tagId = value?.id ?? 0;
48
- }
49
-
50
-
51
- get attrs(): Array<NoteAttr> {
52
- return this.note.attrs.filter(x => x.tagId == this.tagId);
53
- }
54
-
55
- addAttr(attr: Attr): NoteAttr {
56
- if (!this.note)
57
- throw new Error('Cannot call addAttr on NoteTag where note property has not been set');
58
- const na = this.note.addAttr(attr);
59
- na.tag = this.tag;
60
- return na;
61
- }
62
-
63
-
64
- duplicate(): NoteTag {
65
- const output = new NoteTag();
66
- output.noteId = this.noteId;
67
- output.tagId = this.tagId;
68
- return output;
69
- }
70
-
71
-
72
- validate(throwError: boolean = false): boolean {
73
- let output = null;
74
-
75
- if (this.noteId <= 0 && !this.isNew)
76
- output = 'NoteTag noteId must be greater than zero';
77
- else if (this.tagId <= 0)
78
- output = 'NoteTag tagId must be greater than zero';
79
- else if (this.noteId == this.tagId)
80
- output = 'NoteTag cannot link a note to its own tag';
81
-
82
- if (throwError && output != null)
83
- throw Error(output);
84
- return output == null;
85
- }
86
- }
@@ -1,53 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import Space from './Space';
3
-
4
-
5
- test('Gets initiated as new', () => {
6
- const space = new Space();
7
- expect(space.isNew).toBe(true);
8
- });
9
-
10
-
11
- test('Set name marks space as dirty if currently clean', () => {
12
- const space = new Space().clean();
13
- space.name = 'asdf';
14
- expect(space.isDirty).toBe(true);
15
- });
16
-
17
- test('Set name doesnt change space state if new', () => {
18
- const space = new Space().new();
19
- space.name = 'asdf';
20
- expect(space.isNew).toBe(true);
21
- });
22
-
23
- test('Set name doesnt change space state if value not different', () => {
24
- const space = new Space().clean();
25
- space.name = '';
26
- expect(space.isClean).toBe(true);
27
- });
28
-
29
-
30
- test('Space can be initiated with name in constructor', () => {
31
- const space = new Space('hello');
32
- expect(space.name).toBe('hello');
33
- });
34
-
35
- test('can duplicate itself', () => {
36
- const space = new Space('hello').clean();
37
- const copy = space.duplicate();
38
- expect(copy.id).toBe(space.id);
39
- expect(copy.name).toBe(space.name);
40
- expect(copy.state).toBe(space.state);
41
- });
42
-
43
- test('validate fails if not new and id <= 0', () => {
44
- const model = new Space().clean();
45
- model.id = 0;
46
- expect(model.validate()).toBe(false);
47
- });
48
-
49
- test('validate throws error if arg set to true', () => {
50
- const model = new Space().clean();
51
- model.id = 0;
52
- expect(() => model.validate(true)).toThrowError();
53
- });
@@ -1,46 +0,0 @@
1
- 'use strict';
2
-
3
- import ModelWithState from './ModelWithState';
4
-
5
-
6
- export default class Space extends ModelWithState<Space> {
7
- id: number = 0;
8
-
9
-
10
- private _name: string = '';
11
- get name(): string { return this._name; }
12
- set name(value: string) {
13
- if (value !== this._name) {
14
- this._name = value;
15
- if (this.isClean)
16
- this.dirty();
17
- }
18
- }
19
-
20
-
21
- constructor(name: string = '') {
22
- super();
23
- this._name = name;
24
- }
25
-
26
-
27
- duplicate(): Space {
28
- const output = new Space();
29
- output.id = this.id;
30
- output.name = this.name;
31
- output.state = this.state;
32
- return output;
33
- }
34
-
35
-
36
- validate(throwError: boolean = false): boolean {
37
- let output = null;
38
-
39
- if (!this.isNew && this.id <= 0)
40
- output = 'Space id must be greater than zero if in non-new state.';
41
-
42
- if (throwError && output != null)
43
- throw Error(output);
44
- return output == null;
45
- }
46
- }
@@ -1,71 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import Tag from './Tag';
3
-
4
-
5
- test('Gets initiated as new', () => {
6
- const tag = new Tag();
7
- expect(tag.isNew).toBe(true);
8
- });
9
-
10
-
11
- test('Set id marks tag as dirty if currently clean', () => {
12
- const tag = new Tag().clean();
13
- tag.id = 123;
14
- expect(tag.isDirty).toBe(true);
15
- });
16
-
17
- test('Set id doesnt change tag state if new', () => {
18
- const tag = new Tag().new();
19
- tag.id = 123;
20
- expect(tag.isNew).toBe(true);
21
- });
22
-
23
- test('Set id doesnt change tag state if value not different', () => {
24
- const tag = new Tag().clean();
25
- tag.id = 0;
26
- expect(tag.isClean).toBe(true);
27
- });
28
-
29
-
30
- test('Set name marks tag as dirty if currently clean', () => {
31
- const tag = new Tag().clean();
32
- tag.name = 'asdf';
33
- expect(tag.isDirty).toBe(true);
34
- });
35
-
36
- test('Set name doesnt change tag state if new', () => {
37
- const tag = new Tag().new();
38
- tag.name = 'asdf';
39
- expect(tag.isNew).toBe(true);
40
- });
41
-
42
- test('Set name doesnt change tag state if value not different', () => {
43
- const tag = new Tag().clean();
44
- tag.name = '';
45
- expect(tag.isClean).toBe(true);
46
- });
47
-
48
- test('Tag can be initiated with name in constructor', () => {
49
- const tag = new Tag('hello');
50
- expect(tag.name).toBe('hello');
51
- });
52
-
53
- test('can duplicate itself', () => {
54
- const tag = new Tag('hello');
55
- const copy = tag.duplicate();
56
- expect(copy.id).toBe(tag.id);
57
- expect(copy.name).toBe(tag.name);
58
- expect(copy.state).toBe(tag.state);
59
- });
60
-
61
- test('validate fails if not new and id <= 0', () => {
62
- const model = new Tag().clean();
63
- model.id = 0;
64
- expect(model.validate()).toBe(false);
65
- });
66
-
67
- test('validate throws error if arg set to true', () => {
68
- const model = new Tag().clean();
69
- model.id = 0;
70
- expect(() => model.validate(true)).toThrowError();
71
- });
package/src/models/Tag.ts DELETED
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- import ModelWithState from './ModelWithState';
4
-
5
-
6
- export default class Tag extends ModelWithState<Tag> {
7
- private _id: number = 0;
8
- get id(): number { return this._id; }
9
- set id(value: number) {
10
- if (value !== this._id) {
11
- this._id = value;
12
- if (this.isClean)
13
- this.dirty();
14
- }
15
- }
16
-
17
-
18
- private _name: string = '';
19
- get name(): string { return this._name; }
20
- set name(value: string) {
21
- if (value !== this._name) {
22
- this._name = value;
23
- if (this.isClean)
24
- this.dirty();
25
- }
26
- }
27
-
28
-
29
- constructor(name: string = '') {
30
- super();
31
- this._name = name;
32
- }
33
-
34
-
35
- duplicate(): Tag {
36
- const output = new Tag();
37
- output.id = this.id;
38
- output.name = this.name;
39
- output.state = this.state;
40
- return output;
41
- }
42
-
43
-
44
- validate(throwError: boolean = false): boolean {
45
- let output = null;
46
-
47
- if (!this.isNew && this.id <= 0)
48
- output = 'Tag id must be greater than zero if in non-new state.';
49
-
50
- if (throwError && output != null)
51
- throw Error(output);
52
- return output == null;
53
- }
54
- }
@@ -1,108 +0,0 @@
1
- import { expect, test } from 'vitest';
2
- import HttpClient from './HttpClient';
3
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
4
- import Space from '../models/Space';
5
-
6
-
7
- function mockAxiosResponse(status: number, data: any): AxiosResponse<any, any> {
8
- const response = {
9
- data,
10
- status,
11
- statusText: 'hello',
12
- headers: null,
13
- config: null
14
- };
15
- if (status < 200 || status >= 300) {
16
- const error = new Error('Something went wrong');
17
- error['response'] = response;
18
- throw error;
19
- }
20
- return response;
21
- }
22
-
23
- async function mockAxios(config: AxiosRequestConfig<any>): Promise<AxiosResponse<any, any>> {
24
- if (config.method == 'post' && config.url.endsWith('/login')) {
25
- if (config.data.username == 'ValidUser' && config.data.password == 'ValidPassword')
26
- return mockAxiosResponse(200, 'qwer.asdf.zxcv');
27
- return mockAxiosResponse(401, null);
28
- }
29
- if (config.method == 'get' && config.url.endsWith('/spaces')) {
30
- return mockAxiosResponse(200, [
31
- new Space('Space 1'),
32
- new Space('Space 2')
33
- ]);
34
- }
35
- return mockAxiosResponse(404, null);
36
- }
37
-
38
-
39
-
40
-
41
- test('constructor takes api root url', () => {
42
- const client = new HttpClient('https://www.test.com');
43
-
44
- expect(client.url).toBe('https://www.test.com');
45
- });
46
-
47
- test('constructor throws error if url not provided', () => {
48
- expect(() => new HttpClient(null)).toThrowError();
49
- });
50
-
51
- test('If no httpRequester method passed in, then defaults to using axios', () => {
52
- const client = new HttpClient('abcd');
53
-
54
- expect(client['_httpRequester']).toBe(axios);
55
- });
56
-
57
-
58
- test('login asyncronously gets token from web service', async () => {
59
- const client = new HttpClient('abcd', mockAxios);
60
- const loginResult = await client.login('ValidUser', 'ValidPassword');
61
-
62
- expect(loginResult.success).toBe(true);
63
- expect(loginResult.error).toBeNull();
64
- expect(loginResult.token).toBe('qwer.asdf.zxcv');
65
- });
66
-
67
- test('login fails if username and password are invalid', async () => {
68
- const client = new HttpClient('abcd', mockAxios);
69
- const loginResult = await client.login('InvalidUser', 'InvalidPassword');
70
-
71
- expect(loginResult.success).toBe(false);
72
- expect(loginResult.error).toBe('Invalid username & password.');
73
- expect(loginResult.token).toBeNull();
74
- });
75
-
76
- test('login sets token on the client', async () => {
77
- const client = new HttpClient('abcd', mockAxios);
78
- await client.login('ValidUser', 'ValidPassword');
79
-
80
- expect(client.token).toBe('qwer.asdf.zxcv');
81
- });
82
-
83
- test('Token can be manually set if already known', () => {
84
- const client = new HttpClient('abcd', mockAxios);
85
- client.token = 'qwer.asdf.zxcv';
86
-
87
- expect(client.token).toBe('qwer.asdf.zxcv');
88
- });
89
-
90
-
91
- test('getSpaces makes async call to correct URL endpoint, returns space objects', async () => {
92
- let request: AxiosRequestConfig<any> = null;
93
- const client = new HttpClient('abcd', r => {
94
- request = r;
95
- return mockAxios(r);
96
- });
97
- client.token = 'qwer.asdf.zxcv';
98
-
99
- const result = await client.getSpaces();
100
-
101
- expect(request.method).toBe('get');
102
- expect(request.url).toBe('abcd/spaces');
103
- expect(request.data).toBeFalsy();
104
- expect(request.headers.Authorization).toBe('Bearer qwer.asdf.zxcv');
105
- expect(result.length).toBe(2);
106
- expect(result[0].name).toBe('Space 1');
107
- expect(result[1].name).toBe('Space 2');
108
- });