import * as assert from 'assert';
import { svelte } from '../helpers.js';
describe('preprocess', () => {
it('preprocesses entire component', () => {
const source = `
Hello __NAME__!
`;
const expected = `
Hello world!
`;
return svelte.preprocess(source, {
markup: ({ content }) => {
return {
code: content.replace('__NAME__', 'world')
};
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('preprocesses style', () => {
const source = `
$brand
`;
const expected = `
$brand
`;
return svelte.preprocess(source, {
style: ({ content }) => {
return {
code: content.replace('$brand', 'purple')
};
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('preprocesses style asynchronously', () => {
const source = `
$brand
`;
const expected = `
$brand
`;
return svelte.preprocess(source, {
style: ({ content }) => {
return Promise.resolve({
code: content.replace('$brand', 'purple')
});
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('preprocesses script', () => {
const source = `
`;
const expected = `
`;
return svelte.preprocess(source, {
script: ({ content }) => {
return {
code: content.replace('__THE_ANSWER__', '42')
};
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('preprocesses multiple matching tags', () => {
const source = `
`;
const expected = `
`;
return svelte.preprocess(source, {
script: ({ content }) => {
return {
code: content.toLowerCase()
};
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('parses attributes', () => {
const source = `
`;
const expected = `
`;
return svelte.preprocess(source, {
style: ({ attributes }) => {
assert.deepEqual(attributes, {
type: 'text/scss',
'data-foo': 'bar',
bool: true
});
return { code: 'PROCESSED' };
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('provides filename to processing hooks', () => {
const source = `
Hello __MARKUP_FILENAME__!
`;
const expected = `
Hello file.html!
`;
return svelte.preprocess(source, {
filename: 'file.html',
markup: ({ content, filename }) => {
return {
code: content.replace('__MARKUP_FILENAME__', filename)
};
},
style: ({ content, filename }) => {
return {
code: content.replace('__STYLE_FILENAME__', filename)
};
},
script: ({ content, filename }) => {
return {
code: content.replace('__SCRIPT_FILENAME__', filename)
};
}
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
it('ignores null/undefined returned from preprocessor', () => {
const source = `
`;
const expected = `
`;
return svelte.preprocess(source, {
script: () => null
}).then(processed => {
assert.equal(processed.toString(), expected);
});
});
});