Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
logging-scratch-gui
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Goessmann
logging-scratch-gui
Commits
a6fe8585
Commit
a6fe8585
authored
5 years ago
by
Christopher Willis-Ford
Browse files
Options
Downloads
Patches
Plain Diff
add unit tests for defineDynamicBlock
parent
930b2ec0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/unit/util/define-dynamic-block.test.js
+203
-0
203 additions, 0 deletions
test/unit/util/define-dynamic-block.test.js
with
203 additions
and
0 deletions
test/unit/util/define-dynamic-block.test.js
0 → 100644
+
203
−
0
View file @
a6fe8585
import
defineDynamicBlock
from
'
../../../src/lib/define-dynamic-block
'
;
import
ArgumentType
from
'
scratch-vm/src/extension-support/argument-type
'
;
import
BlockType
from
'
scratch-vm/src/extension-support/block-type
'
;
const
MockScratchBlocks
=
{
OUTPUT_SHAPE_HEXAGONAL
:
1
,
OUTPUT_SHAPE_ROUND
:
2
,
OUTPUT_SHAPE_SQUARE
:
3
};
const
categoryInfo
=
{
name
:
'
test category
'
,
color1
:
'
#111
'
,
color2
:
'
#222
'
,
color3
:
'
#333
'
};
const
penIconURI
=
'
data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDAiIGhlaWdodD0iNDAiIHZpZXdCb3g9IjAgMCA0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mi42Ni42NTggMS4xMDIgMS40MTIgMS4yODcgMi4xMDIiIGZpbGw9IiM0Qzk3RkYiLz48cGF0aCBkPSJNMzYuNDk4IDguNzQ4YzAgLjQ2NC0uMTQuODc0LS40MzMgMS4xNjVsLTE5Ljc0MiAxOS42OGMtMi4xMyAyLjExLTQuNjczIDMuNzkzLTcuNTcyIDUuMDFMNC41IDM2LjM4bC45NzQtMi4zMTYgMS45MjUtLjgwOGMyLjg5OC0xLjIxOCA1LjQ0LTIuOSA3LjU3LTUuMDFsMTkuNzQzLTE5LjY4Yy4yOTItLjI5Mi40MzItLjcwMi40MzItMS4xNjUgMC0uNjQ2LS4yNy0xLjQtLjc4LTIuMTIyLjI1LjE3Mi41LjM3Ny43MzcuNjE0Ljg5OC45MDUgMS4zOTYgMS45ODMgMS4zOTYgMi44NTYiIGZpbGw9IiM1NzVFNzUiIG9wYWNpdHk9Ii4xNSIvPjxwYXRoIGQ9Ik0xOC40NSAxMi44M2MwIC41LS40MDQuOTA1LS45MDQuOTA1cy0uOTA1LS40MDUtLjkwNS0uOTA0YzAtLjUuNDA3LS45MDMuOTA2LS45MDMuNSAwIC45MDQuNDA0LjkwNC45MDR6IiBmaWxsPSIjNTc1RTc1Ii8+PC9nPjwvc3ZnPg==
'
;
const
testBlockInfo
=
{
commandWithIcon
:
{
blockType
:
BlockType
.
COMMAND
,
blockIconURI
:
penIconURI
,
text
:
'
command with icon
'
},
commandWithoutIcon
:
{
blockType
:
BlockType
.
COMMAND
,
text
:
'
command without icon
'
},
terminalCommand
:
{
blockType
:
BlockType
.
COMMAND
,
isTerminal
:
true
,
text
:
'
terminal command
'
},
reporter
:
{
blockType
:
BlockType
.
REPORTER
,
text
:
'
reporter
'
},
boolean
:
{
blockType
:
BlockType
.
BOOLEAN
,
text
:
'
Boolean
'
},
hat
:
{
blockType
:
BlockType
.
HAT
,
text
:
'
hat
'
}
};
// similar to goog.mixin from the Closure library
const
mixin
=
function
(
target
,
source
)
{
for
(
const
x
in
source
)
{
target
[
x
]
=
source
[
x
];
}
};
class
MockBlock
{
// mocks the Blockly.Block constructor
static
create
(
blockInfo
,
extendedOpcode
)
{
// mock the Blockly.Block constructor
const
block
=
new
MockBlock
();
const
prototype
=
defineDynamicBlock
(
MockScratchBlocks
,
categoryInfo
,
blockInfo
,
extendedOpcode
);
mixin
(
block
,
prototype
);
block
.
init
();
// bootstrap the mutation<->DOM cycle
block
.
blockInfoText
=
JSON
.
stringify
(
blockInfo
);
const
xmlElement
=
block
.
mutationToDom
();
// parse blockInfo from XML to fill dynamic properties
block
.
domToMutation
(
xmlElement
);
return
block
;
}
jsonInit
(
json
)
{
this
.
result
=
Object
.
assign
({},
json
);
}
interpolate_
()
{
// TODO: add tests for this?
}
setCheckboxInFlyout
(
isEnabled
)
{
this
.
result
.
checkboxInFlyout_
=
isEnabled
;
}
setOutput
(
isEnabled
)
{
this
.
result
.
outputConnection
=
isEnabled
;
// Blockly calls `makeConnection_` here
}
setOutputShape
(
outputShape
)
{
this
.
result
.
outputShape_
=
outputShape
;
}
setNextStatement
(
isEnabled
)
{
this
.
result
.
nextConnection
=
isEnabled
;
// Blockly calls `makeConnection_` here
}
setPreviousStatement
(
isEnabled
)
{
this
.
result
.
previousConnection
=
isEnabled
;
// Blockly calls `makeConnection_` here
}
}
describe
(
'
defineDynamicBlock
'
,
()
=>
{
test
(
'
is a function
'
,
()
=>
{
expect
(
typeof
defineDynamicBlock
).
toBe
(
'
function
'
);
});
test
(
'
can define a command block with an icon
'
,
()
=>
{
const
extendedOpcode
=
'
test.commandWithIcon
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
commandWithIcon
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
extensions
:
[
'
scratch_extension
'
],
inputsInline
:
true
,
nextConnection
:
true
,
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_SQUARE
,
previousConnection
:
true
,
type
:
extendedOpcode
});
});
test
(
'
can define a command block without an icon
'
,
()
=>
{
const
extendedOpcode
=
'
test.commandWithoutIcon
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
commandWithoutIcon
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
// extensions: undefined, // no icon means no extension
inputsInline
:
true
,
nextConnection
:
true
,
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_SQUARE
,
previousConnection
:
true
,
type
:
extendedOpcode
});
});
test
(
'
can define a terminal command
'
,
()
=>
{
const
extendedOpcode
=
'
test.terminal
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
terminalCommand
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
// extensions: undefined, // no icon means no extension
inputsInline
:
true
,
nextConnection
:
false
,
// terminal
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_SQUARE
,
previousConnection
:
true
,
type
:
extendedOpcode
});
});
test
(
'
can define a reporter
'
,
()
=>
{
const
extendedOpcode
=
'
test.reporter
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
reporter
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
checkboxInFlyout_
:
true
,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
// extensions: undefined, // no icon means no extension
inputsInline
:
true
,
// nextConnection: undefined, // reporter
outputConnection
:
true
,
// reporter
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_ROUND
,
// reporter
// previousConnection: undefined, // reporter
type
:
extendedOpcode
});
});
test
(
'
can define a Boolean
'
,
()
=>
{
const
extendedOpcode
=
'
test.boolean
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
boolean
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
// checkboxInFlyout_: undefined,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
// extensions: undefined, // no icon means no extension
inputsInline
:
true
,
// nextConnection: undefined, // reporter
outputConnection
:
true
,
// reporter
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_HEXAGONAL
,
// Boolean
// previousConnection: undefined, // reporter
type
:
extendedOpcode
});
});
test
(
'
can define a hat
'
,
()
=>
{
const
extendedOpcode
=
'
test.hat
'
;
const
block
=
MockBlock
.
create
(
testBlockInfo
.
hat
,
extendedOpcode
);
expect
(
block
.
result
).
toEqual
({
category
:
categoryInfo
.
name
,
colour
:
categoryInfo
.
color1
,
colourSecondary
:
categoryInfo
.
color2
,
colourTertiary
:
categoryInfo
.
color3
,
// extensions: undefined, // no icon means no extension
inputsInline
:
true
,
nextConnection
:
true
,
outputShape_
:
MockScratchBlocks
.
OUTPUT_SHAPE_SQUARE
,
// previousConnection: undefined, // hat
type
:
extendedOpcode
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment