Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
What's new
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jules Sagot--Gentil
Flux articles console
Commits
fa8415a1
Verified
Commit
fa8415a1
authored
Feb 14, 2022
by
Jules Sagot--Gentil
Browse files
feat(console): ConsoleStdout affiche les flux sur la sortie standard
parent
3a3b6906
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/__tests__/console-stdout.ts
0 → 100644
View file @
fa8415a1
import
{
WritableGetSortie
}
from
"
../../test/writable
"
;
import
{
ConsoleStdout
}
from
"
../console
"
;
describe
(
'
La console écrivant sur l
\'
entrée standard
'
,
()
=>
{
const
articleA
=
{
affiche
:
jest
.
fn
(),
egal
:
jest
.
fn
().
mockImplementation
((
a
)
=>
a
==
doublonArticleA
)
};
const
articleB
=
{
affiche
:
jest
.
fn
(),
egal
:
jest
.
fn
().
mockReturnValue
(
false
)
};
const
doublonArticleA
=
{
affiche
:
jest
.
fn
(),
egal
:
jest
.
fn
().
mockReturnValue
(
false
)
};
const
mockFluxA
=
{
getArticles
:
jest
.
fn
().
mockResolvedValue
([
articleA
,
])
};
const
mockFluxB
=
{
getArticles
:
jest
.
fn
().
mockResolvedValue
([
articleB
,
doublonArticleA
,
])
};
let
consoleStdout
:
ConsoleStdout
;
beforeEach
(()
=>
{
jest
.
clearAllMocks
();
consoleStdout
=
new
ConsoleStdout
();
consoleStdout
[
'
output
'
]
=
new
WritableGetSortie
();
});
it
(
'
doit pouvoir afficher les articles d
\'
un flux
'
,
async
()
=>
{
consoleStdout
.
ajouteFlux
(
mockFluxA
);
await
consoleStdout
.
afficheTousFlux
();
expect
(
articleA
.
affiche
.
mock
.
calls
.
length
).
toBe
(
1
);
})
it
(
'
doit pouvoir afficher les articles de deux flux
'
,
async
()
=>
{
consoleStdout
.
ajouteFlux
(
mockFluxA
);
consoleStdout
.
ajouteFlux
(
mockFluxB
);
await
consoleStdout
.
afficheTousFlux
();
expect
(
articleA
.
affiche
.
mock
.
calls
.
length
).
toBe
(
1
);
expect
(
articleB
.
affiche
.
mock
.
calls
.
length
).
toBe
(
1
);
})
it
(
'
doit éliminer les articles doublons
'
,
async
()
=>
{
consoleStdout
.
ajouteFlux
(
mockFluxA
);
consoleStdout
.
ajouteFlux
(
mockFluxB
);
await
consoleStdout
.
afficheTousFlux
();
expect
(
doublonArticleA
.
affiche
.
mock
.
calls
.
length
).
toBe
(
0
);
})
})
src/console.ts
View file @
fa8415a1
import
{
Writable
}
from
'
stream
'
;
import
{
Article
}
from
'
./article
'
;
import
{
Flux
}
from
'
./flux
'
;
interface
Console
{
ajouteFlux
(
flux
:
Flux
):
void
;
afficheTousFlux
():
void
;
afficheTousFlux
():
Promise
<
void
>
;
}
export
class
ConsoleStdout
implements
Console
{
private
output
:
Writable
=
process
.
stdout
;
private
flux
:
Flux
[]
=
[];
private
articlesAffiches
:
Article
[]
=
[];
public
ajouteFlux
(
flux
:
Flux
):
void
{
this
.
flux
.
push
(
flux
);
};
public
async
afficheTousFlux
():
Promise
<
void
>
{
const
articles
=
(
await
Promise
.
all
(
this
.
flux
.
map
((
f
)
=>
f
.
getArticles
()))
).
flat
();
for
(
const
article
of
articles
)
{
if
(
this
.
articleDejaAffiche
(
article
))
{
continue
;
}
else
{
this
.
afficheArticle
(
article
);
this
.
articlesAffiches
.
push
(
article
);
}
}
};
private
afficheArticle
(
article
:
Article
)
{
this
.
output
.
write
(
`Article n°
${
this
.
articlesAffiches
.
length
+
1
}
\n`
);
article
.
affiche
(
this
.
output
);
this
.
output
.
write
(
'
------
\n\n
'
);
};
private
articleDejaAffiche
(
article
:
Article
)
{
return
!!
this
.
articlesAffiches
.
find
((
a
)
=>
a
.
egal
(
article
));
};
}
Write
Preview
Supports
Markdown
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