1+ import { ExpirationCompleteEvent } from '@hn-hub/common' ;
2+ import { Message } from 'node-nats-streaming' ;
3+ import { Story , StoryDoc } from '../../../models/stories' ;
4+ import { natsWrapper } from '../../../nats-wrapper' ;
5+ import { ExpirationCompleteListener } from '../expiration-complete-listener' ;
6+
7+ const storyData = [
8+ {
9+ "comments" : [
10+ 24124834
11+ ] ,
12+ "isExpired" : false ,
13+ "title" : "Mozilla lays off 250 employees while it refocuses on commercial products" ,
14+ "url" : "https://blog.mozilla.org/blog/2020/08/11/changing-world-changing-mozilla/" ,
15+ "score" : 1583 ,
16+ "createdAt" : 1597154636 ,
17+ "user" : "rebelwebmaster" ,
18+ "storyId" : 24120336
19+ } ,
20+ {
21+ "comments" : [
22+ 24135784
23+ ] ,
24+ "isExpired" : false ,
25+ "title" : "Mozilla Lifeboat" ,
26+ "url" : "https://mozillalifeboat.com" ,
27+ "score" : 1432 ,
28+ "createdAt" : 1597256213 ,
29+ "user" : "gkoberger" ,
30+ "storyId" : 24135032
31+ }
32+ ] ;
33+
34+ const setup = async ( ) => {
35+ const listener = new ExpirationCompleteListener ( natsWrapper . client ) ;
36+ const storyBuildObject : any [ ] = [ ] ;
37+ storyData . forEach ( story => {
38+ const st = Story . build ( {
39+ by : story . user ,
40+ score : story . score ,
41+ time : story . createdAt + '' ,
42+ title : story . title ,
43+ url : story . url ,
44+ kids : story . comments ! ,
45+ id : story . storyId
46+ } ) ; // building the story object
47+ storyBuildObject . push ( st . save ( ) ) ; // chainning it into the Promise array
48+ } ) ;
49+ // Saving all records at once
50+ const stories : Array < StoryDoc > = await Promise . all ( storyBuildObject ) ;
51+ const data : ExpirationCompleteEvent [ 'data' ] = {
52+ stories : stories . map ( e => e . storyId )
53+ } ;
54+
55+ // @ts -ignore
56+ const msg : Message = {
57+ ack : jest . fn ( )
58+ } ;
59+
60+ return { listener, stories, data, msg } ;
61+ } ;
62+
63+
64+ it ( 'delete all the given stories' , async ( ) => {
65+ const { listener, data, msg } = await setup ( ) ;
66+
67+ await listener . onMessage ( data , msg ) ;
68+
69+ const updatedStory = await Story . find ( { } ) ;
70+
71+ expect ( updatedStory . length ) . toEqual ( 0 ) ;
72+ } ) ;
73+
74+ it ( 'ack the message' , async ( ) => {
75+ const { listener, data, msg } = await setup ( ) ;
76+
77+ await listener . onMessage ( data , msg ) ;
78+
79+ expect ( msg . ack ) . toHaveBeenCalled ( ) ;
80+ } ) ;
0 commit comments