Skip to content

Commit

Permalink
add unit test for render
Browse files Browse the repository at this point in the history
  • Loading branch information
littlejiancc committed Sep 28, 2021
1 parent de98051 commit 28afced
Showing 1 changed file with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
package stages

import (
"bou.ke/monkey"
"github.com/erda-project/erda/bundle"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,3 +67,211 @@ func TestFindFirstLastStepInGroup(t *testing.T) {
assert.Equal(t, v.wantLast, lastStepID)
}
}

// TestRenderItemMoveStagesFormFrontTarget [1,2] 3 move 2 to the front of 3
func TestRenderItemMoveStagesFormFrontTarget(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) {
return &apistructs.TestPlanV2Step{
SceneSetID: 1,
PreID: 1,
PlanID: 1,
GroupID: 1,
ID: 2,
}, nil
})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 1,
DropGroupKey: 3,
DragKey: 2,
DropKey: 3,
Position: -1,
},
TestPlanId: 1,
},
},
}
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "MoveTestPlansV2Step", func(*bundle.Bundle, apistructs.TestPlanV2StepMoveRequest) error {
return nil
})
if i.RenderItemMoveStagesForm() != nil {
t.Error("fail")
}
}

// TestRenderItemMoveStagesFormBehindTarget [1,2] [3] move 2 to the behind of 3
func TestRenderItemMoveStagesFormBehindTarget(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) {
return &apistructs.TestPlanV2Step{
SceneSetID: 1,
PreID: 1,
PlanID: 1,
GroupID: 1,
ID: 2,
}, nil
})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 1,
DropGroupKey: 3,
DragKey: 2,
DropKey: 3,
Position: 1,
},
TestPlanId: 1,
},
},
}
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "MoveTestPlansV2Step", func(*bundle.Bundle, apistructs.TestPlanV2StepMoveRequest) error {
return nil
})
if i.RenderItemMoveStagesForm() != nil {
t.Error("fail")
}
}

// TestRenderItemMoveStagesFormBehindTarget [1,2] [3] move 1 to the front of 2
func TestRenderItemMoveStagesFormNoChange(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) {
return &apistructs.TestPlanV2Step{
PreID: 1,
GroupID: 1,
ID: 1,
}, nil
})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 1,
DropGroupKey: 1,
DragKey: 1,
DropKey: 2,
Position: -1,
},
TestPlanId: 1,
},
},
}
if i.RenderItemMoveStagesForm() != nil {
t.Error("fail")
}
}

// TestRenderItemMoveStagesFormBehindTarget2 [1,2] [3] move 2 to the behind of 1
func TestRenderItemMoveStagesFormNoChange2(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "GetTestPlanV2Step", func(*bundle.Bundle, uint64) (*apistructs.TestPlanV2Step, error) {
return &apistructs.TestPlanV2Step{
PreID: 1,
GroupID: 1,
ID: 2,
}, nil
})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 1,
DropGroupKey: 1,
DragKey: 2,
DropKey: 1,
Position: 1,
},
TestPlanId: 1,
},
},
}
if i.RenderItemMoveStagesForm() != nil {
t.Error("fail")
}
}

// TestRenderGroupMoveStagesFormNoChange [1,2] [3] move [1,2] to the front of [3]
func TestRenderGroupMoveStagesFormNoChange(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(bdl *bundle.Bundle, planID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) {
if groupID == 1 {
return []*apistructs.TestPlanV2Step{
{ID: 1, PreID: 0},
{ID: 2, PreID: 1},
}, nil
}
return []*apistructs.TestPlanV2Step{
{ID: 3, PreID: 2},
}, nil

})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 1,
DropGroupKey: 3,
Position: -1,
},
TestPlanId: 1,
},
},
}
if i.RenderGroupMoveStagesForm() != nil {
t.Error("fail")
}
}

// TestRenderGroupMoveStagesFormNoChange2 [1,2] [3] move [3] to the behind of [1,2]
func TestRenderGroupMoveStagesFormNoChange2(t *testing.T) {
var bdl *bundle.Bundle
monkey.PatchInstanceMethod(reflect.TypeOf(bdl), "ListTestPlanV2Step", func(bdl *bundle.Bundle, planID, groupID uint64) ([]*apistructs.TestPlanV2Step, error) {
if groupID == 3 {
return []*apistructs.TestPlanV2Step{
{ID: 3, PreID: 2},
}, nil
}
return []*apistructs.TestPlanV2Step{
{ID: 1, PreID: 0},
{ID: 2, PreID: 1},
}, nil

})
defer monkey.UnpatchAll()

i := ComponentStageForm{
ctxBdl: protocol.ContextBundle{},
CommonStageForm: CommonStageForm{
State: State{
DragParams: DragParams{
DragGroupKey: 3,
DropGroupKey: 1,
Position: 1,
},
TestPlanId: 1,
},
},
}
if i.RenderGroupMoveStagesForm() != nil {
t.Error("fail")
}
}

0 comments on commit 28afced

Please sign in to comment.