Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
truenas-rk3588
webui
Commits
27545860
Commit
27545860
authored
1 year ago
by
Boris Vasilenko
Browse files
Options
Download
Email Patches
Plain Diff
NAS-125092: Add unit tests
parent
bdf92206
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/app/services/error-accumulator.service.spec.ts
+56
-0
src/app/services/error-accumulator.service.spec.ts
src/app/services/error-accumulator.service.ts
+3
-3
src/app/services/error-accumulator.service.ts
src/app/services/error-handler.service.ts
+2
-2
src/app/services/error-handler.service.ts
with
61 additions
and
5 deletions
+61
-5
src/app/services/error-accumulator.service.spec.ts
0 → 100644
View file @
27545860
import
{
Injector
}
from
'
@angular/core
'
;
import
{
createServiceFactory
,
mockProvider
,
SpectatorService
}
from
'
@ngneat/spectator/jest
'
;
import
{
KiB
}
from
'
app/constants/bytes.constant
'
;
import
{
ErrorAccumulatorService
}
from
'
app/services/error-accumulator.service
'
;
describe
(
'
ErrorAccumulatorService
'
,
()
=>
{
let
spectator
:
SpectatorService
<
ErrorAccumulatorService
>
;
const
createService
=
createServiceFactory
({
service
:
ErrorAccumulatorService
,
providers
:
[
mockProvider
(
Injector
,
{
get
:
jest
.
fn
(),
}),
],
});
beforeEach
(()
=>
{
spectator
=
createService
();
});
describe
(
'
appendError
'
,
()
=>
{
it
(
'
appends error to logs list
'
,
()
=>
{
jest
.
spyOn
(
spectator
.
service
,
'
appendError
'
);
spectator
.
service
.
appendError
(
'
error message 1
'
);
spectator
.
service
.
appendError
(
'
error message 2
'
);
expect
(
spectator
.
service
.
getErrorLogs
()).
toBe
(
'
error message 1
\n
error message 2
'
,
);
spectator
.
service
.
appendError
(
'
error message 3
'
);
expect
(
spectator
.
service
.
getErrorLogs
()).
toBe
(
'
error message 1
\n
error message 2
\n
error message 3
'
,
);
});
it
(
'
truncates logs when overflowing
'
,
()
=>
{
jest
.
spyOn
(
spectator
.
service
,
'
appendError
'
);
const
fakeErrors
=
{
errorA
:
'
a
'
.
repeat
(
KiB
*
10
),
errorB
:
'
b
'
.
repeat
(
KiB
*
10
),
errorC
:
'
c
'
.
repeat
(
KiB
*
10
),
errorD
:
'
d
'
.
repeat
(
KiB
*
10
),
errorE
:
'
e
'
.
repeat
(
KiB
*
10
),
errorF
:
'
f
'
.
repeat
(
KiB
*
10
),
};
Object
.
values
(
fakeErrors
).
forEach
((
error
)
=>
spectator
.
service
.
appendError
(
error
));
expect
(
spectator
.
service
.
getErrorLogs
()).
toBe
(
`
${
fakeErrors
.
errorC
}
\n
${
fakeErrors
.
errorD
}
\n
${
fakeErrors
.
errorE
}
\n
${
fakeErrors
.
errorF
}
`
,
);
});
});
});
This diff is collapsed.
Click to expand it.
src/app/services/error-accumulator.service.ts
View file @
27545860
...
...
@@ -6,9 +6,9 @@ import { KiB } from 'app/constants/bytes.constant';
})
export
class
ErrorAccumulatorService
{
private
errorLogs
:
string
[]
=
[];
private
max
SizeLogs
=
KiB
*
50
;
private
max
Length
=
KiB
*
50
;
save
Error
(
errorMsg
:
string
):
void
{
append
Error
(
errorMsg
:
string
):
void
{
this
.
errorLogs
.
push
(
errorMsg
);
this
.
truncateErrors
();
}
...
...
@@ -18,7 +18,7 @@ export class ErrorAccumulatorService {
}
private
truncateErrors
():
void
{
if
(
JSON
.
stringify
(
this
.
errorLogs
).
length
>
this
.
max
SizeLogs
)
{
if
(
JSON
.
stringify
(
this
.
errorLogs
).
length
>
this
.
max
Length
)
{
this
.
errorLogs
.
shift
();
this
.
truncateErrors
();
}
...
...
This diff is collapsed.
Click to expand it.
src/app/services/error-handler.service.ts
View file @
27545860
...
...
@@ -43,9 +43,9 @@ export class ErrorHandlerService implements ErrorHandler {
if
(
parsedError
)
{
error
=
parsedError
;
if
(
Array
.
isArray
(
parsedError
))
{
parsedError
.
forEach
((
err
)
=>
this
.
errorAccumulator
.
save
Error
(
err
.
message
));
parsedError
.
forEach
((
err
)
=>
this
.
errorAccumulator
.
append
Error
(
err
.
message
));
}
else
{
this
.
errorAccumulator
.
save
Error
(
parsedError
.
message
);
this
.
errorAccumulator
.
append
Error
(
parsedError
.
message
);
}
}
this
.
logToSentry
(
error
);
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help