2017-10-31 11:12:26 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Sensing = require('../../src/blocks/scratch3_sensing');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
2017-12-28 12:31:23 -05:00
|
|
|
const Sprite = require('../../src/sprites/sprite');
|
|
|
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
2017-10-31 11:12:26 -04:00
|
|
|
|
|
|
|
test('getPrimitives', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const s = new Sensing(rt);
|
|
|
|
t.type(s.getPrimitives(), 'object');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-11-08 10:37:38 -05:00
|
|
|
test('ask and answer with a hidden target', t => {
|
2017-10-31 11:12:26 -04:00
|
|
|
const rt = new Runtime();
|
|
|
|
const s = new Sensing(rt);
|
2017-11-08 10:37:38 -05:00
|
|
|
const util = {target: {visible: false}};
|
2017-10-31 11:12:26 -04:00
|
|
|
|
|
|
|
const expectedQuestion = 'a question';
|
|
|
|
const expectedAnswer = 'the answer';
|
|
|
|
|
|
|
|
// Test is written out of order because of promises, follow the (#) comments.
|
|
|
|
rt.addListener('QUESTION', question => {
|
|
|
|
// (2) Assert the question is correct, then emit the answer
|
|
|
|
t.strictEqual(question, expectedQuestion);
|
|
|
|
rt.emit('ANSWER', expectedAnswer);
|
|
|
|
});
|
|
|
|
|
|
|
|
// (1) Emit the question.
|
2017-11-08 10:37:38 -05:00
|
|
|
const promise = s.askAndWait({QUESTION: expectedQuestion}, util);
|
2017-10-31 11:12:26 -04:00
|
|
|
|
|
|
|
// (3) Ask block resolves after the answer is emitted.
|
|
|
|
promise.then(() => {
|
|
|
|
t.strictEqual(s.getAnswer(), expectedAnswer);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
2017-11-08 10:37:38 -05:00
|
|
|
|
|
|
|
test('ask and answer with a visible target', t => {
|
|
|
|
const rt = new Runtime();
|
|
|
|
const s = new Sensing(rt);
|
|
|
|
const util = {target: {visible: true}};
|
|
|
|
|
|
|
|
const expectedQuestion = 'a question';
|
|
|
|
const expectedAnswer = 'the answer';
|
|
|
|
|
|
|
|
rt.removeAllListeners('SAY'); // Prevent say blocks from executing
|
|
|
|
|
|
|
|
rt.addListener('SAY', (target, type, question) => {
|
|
|
|
// Should emit SAY with the question
|
|
|
|
t.strictEqual(question, expectedQuestion);
|
|
|
|
});
|
|
|
|
|
|
|
|
rt.addListener('QUESTION', question => {
|
|
|
|
// Question should be blank for a visible target
|
|
|
|
t.strictEqual(question, '');
|
|
|
|
|
|
|
|
// Remove the say listener and add a new one to assert bubble is cleared
|
|
|
|
// by setting say to empty string after answer is received.
|
|
|
|
rt.removeAllListeners('SAY');
|
|
|
|
rt.addListener('SAY', (target, type, text) => {
|
|
|
|
t.strictEqual(text, '');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
rt.emit('ANSWER', expectedAnswer);
|
|
|
|
});
|
|
|
|
|
|
|
|
s.askAndWait({QUESTION: expectedQuestion}, util);
|
|
|
|
});
|
2017-12-28 12:31:23 -05:00
|
|
|
|
|
|
|
test('set drag mode', t => {
|
2017-12-28 12:31:23 -05:00
|
|
|
const runtime = new Runtime();
|
|
|
|
runtime.requestTargetsUpdate = () => {}; // noop for testing
|
|
|
|
const sensing = new Sensing(runtime);
|
|
|
|
const s = new Sprite();
|
|
|
|
const rt = new RenderedTarget(s, runtime);
|
2017-12-28 12:31:23 -05:00
|
|
|
|
2017-12-28 12:31:23 -05:00
|
|
|
sensing.setDragMode({DRAG_MODE: 'not draggable'}, {target: rt});
|
|
|
|
t.strictEqual(rt.draggable, false);
|
2017-12-28 12:31:23 -05:00
|
|
|
|
2017-12-28 12:31:23 -05:00
|
|
|
sensing.setDragMode({DRAG_MODE: 'draggable'}, {target: rt});
|
|
|
|
t.strictEqual(rt.draggable, true);
|
2017-12-28 12:31:23 -05:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|