Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea9e2f2

Browse files
authored
test: SSE wire level tests (#765)
1 parenta7448a5 commitea9e2f2

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

‎src/test/repository.test.ts‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function createMockEventSource() {
4343
returneventSource;
4444
}
4545

46+
functioncreateSSEResponse(events:Array<{event:string;data:any}>){
47+
returnevents
48+
.map((e)=>{
49+
constdataStr=typeofe.data==='string' ?e.data :JSON.stringify(e.data);
50+
return`event:${e.event}\ndata:${dataStr}\n\n`;
51+
})
52+
.join('');
53+
}
54+
4655
test('should fetch from endpoint',(t)=>
4756
newPromise((resolve)=>{
4857
consturl='http://unleash-test-0.app';
@@ -1970,3 +1979,143 @@ test('setMode should be no-op when repository is stopped', async (t) => {
19701979
awaitrepo.setMode('streaming');
19711980
t.is(repo.getMode(),'polling');
19721981
});
1982+
1983+
test('SSE with HTTP mocking - should process unleash-connected event',async(t)=>{
1984+
consturl='http://unleash-test-sse-http.app';
1985+
constfeature={
1986+
name:'test-feature',
1987+
enabled:true,
1988+
strategies:[
1989+
{
1990+
name:'default',
1991+
},
1992+
],
1993+
};
1994+
1995+
constsseResponse=createSSEResponse([
1996+
{
1997+
event:'unleash-connected',
1998+
data:{
1999+
events:[
2000+
{
2001+
type:'hydration',
2002+
eventId:1,
2003+
features:[feature],
2004+
segments:[],
2005+
},
2006+
],
2007+
},
2008+
},
2009+
]);
2010+
2011+
nock(url)
2012+
.persist()
2013+
.get('/client/streaming')
2014+
.reply(200,sseResponse,{'Content-Type':'text/event-stream'});
2015+
2016+
conststorageProvider:StorageProvider<ClientFeaturesResponse>=newInMemStorageProvider();
2017+
2018+
constrepo=newRepository({
2019+
url,
2020+
appName,
2021+
instanceId,
2022+
connectionId,
2023+
refreshInterval:10,
2024+
//@ts-expect-error
2025+
bootstrapProvider:newDefaultBootstrapProvider({}),
2026+
storageProvider,
2027+
mode:{type:'streaming'},
2028+
});
2029+
2030+
constchangedEvent=newPromise<void>((resolve)=>{
2031+
repo.once('changed',resolve);
2032+
});
2033+
2034+
awaitrepo.start();
2035+
awaitchangedEvent;
2036+
2037+
consttoggles=repo.getToggles();
2038+
t.is(toggles.length,1);
2039+
t.is(toggles[0].name,'test-feature');
2040+
t.is(toggles[0].enabled,true);
2041+
2042+
repo.stop();
2043+
});
2044+
2045+
test('SSE with HTTP mocking - should process unleash-updated event',async(t)=>{
2046+
consturl='http://unleash-test-sse-http-updated.app';
2047+
constfeature={
2048+
name:'test-feature',
2049+
enabled:false,
2050+
strategies:[
2051+
{
2052+
name:'default',
2053+
},
2054+
],
2055+
};
2056+
2057+
constsseResponse=createSSEResponse([
2058+
{
2059+
event:'unleash-connected',
2060+
data:{
2061+
events:[
2062+
{
2063+
type:'hydration',
2064+
eventId:1,
2065+
features:[feature],
2066+
segments:[],
2067+
},
2068+
],
2069+
},
2070+
},
2071+
{
2072+
event:'unleash-updated',
2073+
data:{
2074+
events:[
2075+
{
2076+
type:'feature-updated',
2077+
eventId:2,
2078+
feature:{ ...feature,enabled:true},
2079+
},
2080+
],
2081+
},
2082+
},
2083+
]);
2084+
2085+
nock(url)
2086+
.persist()
2087+
.get('/client/streaming')
2088+
.reply(200,sseResponse,{'Content-Type':'text/event-stream'});
2089+
2090+
conststorageProvider:StorageProvider<ClientFeaturesResponse>=newInMemStorageProvider();
2091+
2092+
constrepo=newRepository({
2093+
url,
2094+
appName,
2095+
instanceId,
2096+
connectionId,
2097+
refreshInterval:10,
2098+
//@ts-expect-error
2099+
bootstrapProvider:newDefaultBootstrapProvider({}),
2100+
storageProvider,
2101+
mode:{type:'streaming'},
2102+
});
2103+
2104+
letchangeCount=0;
2105+
constchangedEvents=newPromise<void>((resolve)=>{
2106+
repo.on('changed',()=>{
2107+
changeCount++;
2108+
if(changeCount===2){
2109+
resolve();
2110+
}
2111+
});
2112+
});
2113+
2114+
awaitrepo.start();
2115+
awaitchangedEvents;
2116+
2117+
consttoggles=repo.getToggles();
2118+
t.is(toggles[0].enabled,true);
2119+
2120+
repo.stop();
2121+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp