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

Commit79f0fba

Browse files
committed
setup getProgress tests
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent7766d82 commit79f0fba

File tree

3 files changed

+125
-25
lines changed

3 files changed

+125
-25
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import*asTTfrom'typings/tutorial'
2+
importgetProgressfrom'./getProgress'
3+
4+
constlevels:TT.Level[]=[
5+
{
6+
id:'1',
7+
title:'',
8+
summary:'',
9+
content:'',
10+
steps:[
11+
{
12+
id:'1.1',
13+
content:'First',
14+
setup:{commits:[]},
15+
},
16+
{
17+
id:'1.2',
18+
content:'Second',
19+
setup:{commits:[]},
20+
},
21+
{
22+
id:'1.3',
23+
content:'Last',
24+
setup:{commits:[]},
25+
},
26+
],
27+
},
28+
{
29+
id:'2',
30+
title:'',
31+
summary:'',
32+
content:'',
33+
steps:[
34+
{
35+
id:'2.1',
36+
content:'First',
37+
setup:{commits:[]},
38+
},
39+
{
40+
id:'2.2',
41+
content:'Second',
42+
setup:{commits:[]},
43+
},
44+
{
45+
id:'2.3',
46+
content:'Last',
47+
setup:{commits:[]},
48+
},
49+
],
50+
},
51+
]
52+
53+
describe('getProgress',()=>{
54+
it('should accept no progress',()=>{
55+
constposition={levelId:'1',stepId:'1.1',complete:false}
56+
constresult=getProgress(levels,position)
57+
expect(result).toBe(0)
58+
})
59+
it('should account for a completed level that has not continued',()=>{
60+
constposition={levelId:'1',stepId:'1.3',complete:true}
61+
constresult=getProgress(levels,position)
62+
expect(result).toBe(50)
63+
})
64+
it('should use the last completed level',()=>{
65+
constposition={levelId:'2',stepId:'2.1',complete:false}
66+
constresult=getProgress(levels,position)
67+
expect(result).toBe(50)
68+
})
69+
it('should work if a level has no steps',()=>{
70+
constnoStepLevels=[
71+
{ ...levels[0],steps:[]},
72+
{ ...levels[1],steps:[]},
73+
]
74+
constposition={levelId:'1',stepId:null,complete:false}
75+
constresult=getProgress(noStepLevels,position)
76+
expect(result).toBe(0)
77+
})
78+
it('should work if a level has no steps but completed',()=>{
79+
constnoStepLevels=[
80+
{ ...levels[0],steps:[]},
81+
{ ...levels[1],steps:[]},
82+
]
83+
constposition={levelId:'1',stepId:null,complete:true}
84+
constresult=getProgress(noStepLevels,position)
85+
expect(result).toBe(50)
86+
})
87+
it('should accept a completed tutorial',()=>{
88+
constposition={levelId:'2',stepId:'2.3',complete:true}
89+
constresult=getProgress(levels,position)
90+
expect(result).toBe(100)
91+
})
92+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import*asTfrom'typings'
2+
import*asTTfrom'typings/tutorial'
3+
4+
constgetProgress=(levels:TT.Level[]=[],position:T.Position):number=>{
5+
letprogress=0
6+
letisLevelComplete=false
7+
if(levels&&levels.length){
8+
consttotalLevels=levels.length
9+
constfindLevel=(level:TT.Level)=>level.id===position.levelId
10+
constcurrentLevel:TT.Level|undefined=levels.find(findLevel)
11+
letcurrentLevelIndex:number=levels.findIndex(findLevel)
12+
if(!currentLevel){
13+
thrownewError('Invalid level')
14+
}
15+
// check if the level is complete
16+
if(position.stepId&&currentLevel.steps&&currentLevel.steps.length){
17+
constlastStepInLevel:TT.Step|null=currentLevel.steps[currentLevel.steps.length]
18+
isLevelComplete=position.complete&&lastStepInLevel.id===position.stepId
19+
}else{
20+
isLevelComplete=position.complete
21+
}
22+
progress=Math.round(((currentLevelIndex+(isLevelComplete ?1 :0))/totalLevels)*100)
23+
}
24+
returnprogress
25+
}
26+
27+
exportdefaultgetProgress

‎web-app/src/containers/Start/index.tsx

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Button from '../../components/Button'
88
import{Theme}from'../../styles/theme'
99
import{ADMIN_MODE}from'../../environment'
1010
importAdminTogglefrom'../../services/admin/AdminToggle'
11+
importgetProgressfrom'./getProgress'
1112

1213
conststyles={
1314
page:(theme:Theme)=>({
@@ -95,8 +96,8 @@ const styles = {
9596
interfaceProps{
9697
onContinue():void
9798
onNew():void
98-
tutorial?:TT.Tutorial
99-
progress?:number
99+
tutorial:TT.Tutorial|null
100+
progress:number
100101
}
101102

102103
exportconstStartPage=(props:Props)=>(
@@ -122,7 +123,7 @@ export const StartPage = (props: Props) => (
122123
<buttononClick={props.onContinue}css={styles.buttonLarge}>
123124
Continue Tutorial
124125
<divcss={styles.continueTitle}>"{props.tutorial.summary.title}"</div>
125-
<Progressstyle={{marginLeft:'1rem'}}percent={props.progress||0}hasBordersize="large"/>
126+
<Progressstyle={{marginLeft:'1rem'}}percent={props.progress}hasBordersize="large"/>
126127
</button>
127128
</div>
128129
)}
@@ -141,32 +142,12 @@ interface ContainerProps {
141142
}
142143

143144
constStartPageContainer=({ context, send}:ContainerProps)=>{
144-
consttutorial=context.tutorial||undefined
145-
letprogress
146-
letisLevelComplete=false
147-
if(tutorial){
148-
consttotalLevels=tutorial.levels.length
149-
const{ position}=context
150-
constfindLevel=(level:TT.Level)=>level.id===position.levelId
151-
constcurrentLevel:TT.Level|undefined=tutorial.levels.find(findLevel)
152-
letcurrentLevelIndex:number=tutorial.levels.findIndex(findLevel)
153-
if(!currentLevel){
154-
thrownewError('Invalid level')
155-
}
156-
// check if the level is complete
157-
if(position.stepId&&currentLevel.steps&&currentLevel.steps.length){
158-
constlastStepInLevel:TT.Step|null=currentLevel.steps[currentLevel.steps.length]
159-
isLevelComplete=position.complete&&lastStepInLevel.id===position.stepId
160-
}else{
161-
isLevelComplete=position.complete
162-
}
163-
progress=Math.round(((currentLevelIndex+(isLevelComplete ?1 :0))/totalLevels)*100)
164-
}
145+
constprogress:number=getProgress(context?.tutorial?.levels,context.position)
165146
return(
166147
<StartPage
167148
onContinue={()=>send({type:'CONTINUE_TUTORIAL'})}
168149
onNew={()=>send({type:'NEW_TUTORIAL'})}
169-
tutorial={tutorial}
150+
tutorial={context.tutorial}
170151
progress={progress}
171152
/>
172153
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp