Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some TS component blueprint tests #20771

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ blueprints/*/*files/**/*.js
blueprints-js/*/*files/**/*.js
blueprints/*/*files/**/*.ts
node-tests/fixtures/**/*.js
node-tests/fixtures/**/*.ts
/docs/
dist/
tmp/
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,14 @@ jobs:
name: Blueprint Tests
runs-on: ubuntu-latest
needs: [lint]
strategy:
matrix:
script-name: ['js', 'js-from-ts', 'ts']
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/setup
- name: test
run: pnpm test:blueprints
run: pnpm test:blueprints:${{matrix.script-name}}

browser-test:
name: Browser Tests (Firefox)
Expand Down
2 changes: 1 addition & 1 deletion blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = {

normalizeEntityName(entityName) {
return normalizeEntityName(
entityName.replace(/\.js$/, '') //Prevent generation of ".js.js" files
entityName.replace(/\.(js|ts)$/, '') //Prevent generation of ".js.js" and ".ts.ts" files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does js.js happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When people add an extension when generating a component: ember g component-class foo.js. I just noticed when copying one of the tests and adjusting it for .ts.

Other ts blueprints will have the same issue.

);
},

Expand Down
290 changes: 290 additions & 0 deletions node-tests/blueprints-ts/component-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
'use strict';

const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
const setupTestHooks = blueprintHelpers.setupTestHooks;
const emberNew = blueprintHelpers.emberNew;
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;

const chai = require('ember-cli-blueprint-test-helpers/chai');
const expect = chai.expect;

const fixture = require('../helpers/fixture');

function glimmerComponentContents(componentName = 'Foo') {
return `import Component from '@glimmer/component';

export interface ${componentName}Signature {
// The arguments accepted by the component
Args: {};
// Any blocks yielded by the component
Blocks: {
default: []
};
// The element to which \`...attributes\` is applied in the component template
Element: null;
}

export default class ${componentName} extends Component<${componentName}Signature> {}
`;
}

const emberComponentContents = `import Component from '@ember/component';


export default Component.extend({});
`;

const templateOnlyContents = `import templateOnly from '@ember/component/template-only';

export interface FooSignature {
// The arguments accepted by the component
Args: {};
// Any blocks yielded by the component
Blocks: {
default: []
};
// The element to which \`...attributes\` is applied in the component template
Element: null;
}

export default templateOnly<FooSignature>();
`;

describe('TS Blueprint: component', function () {
setupTestHooks(this);

describe('in app', function () {
beforeEach(function () {
return emberNew({ cliArgs: ['--typescript'] });
});

it('component foo', function () {
return emberGenerateDestroy(['component', 'foo'], (_file) => {
expect(_file('app/components/foo.ts')).to.not.exist;
expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
});
});

// Octane default
it('component foo --component-structure=flat --component-class=@glimmer/component', function () {
return emberGenerateDestroy(
[
'component',
'--component-structure',
'flat',
'--component-class',
'@glimmer/component',
'foo',
],
(_file) => {
expect(_file('app/components/foo.ts')).to.equal(glimmerComponentContents());
expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --component-structure=flat', function () {
return emberGenerateDestroy(
['component', '--component-structure', 'flat', 'foo'],
(_file) => {
expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --component-structure=nested', function () {
return emberGenerateDestroy(
['component', '--component-structure', 'nested', 'foo'],
(_file) => {
expect(_file('app/components/foo/index.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --component-class=@ember/component', function () {
return emberGenerateDestroy(
['component', '--component-class', '@ember/component', 'foo'],
(_file) => {
expect(_file('app/components/foo.ts')).to.equal(emberComponentContents);

expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --component-class=@glimmer/component', function () {
return emberGenerateDestroy(
['component', '--component-class', '@glimmer/component', 'foo'],
(_file) => {
expect(_file('app/components/foo.ts')).to.equal(glimmerComponentContents());

expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --component-class=@ember/component/template-only', function () {
return emberGenerateDestroy(
['component', '--component-class', '@ember/component/template-only', 'foo'],
(_file) => {
expect(_file('app/components/foo.ts')).to.equal(templateOnlyContents);

expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
}
);
});

it('component foo --no-component-class', function () {
return emberGenerateDestroy(['component', '--no-component-class', 'foo'], (_file) => {
expect(_file('app/components/foo.ts')).to.not.exist;

expect(_file('app/components/foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo',
componentInvocation: 'Foo',
},
})
);
});
});

it('component x-foo', function () {
return emberGenerateDestroy(['component', 'x-foo'], (_file) => {
expect(_file('app/components/x-foo.ts')).to.not.exist;
expect(_file('app/components/x-foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/x-foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'x-foo',
componentInvocation: 'XFoo',
},
})
);
});
});

it('component x-foo.ts', function () {
return emberGenerateDestroy(['component', 'x-foo.ts'], (_file) => {
expect(_file('app/components/x-foo.ts')).to.not.exist;
expect(_file('app/components/x-foo.js.js')).to.not.exist;
expect(_file('app/templates/components/x-foo.ts.hbs')).to.not.exist;
expect(_file('tests/integration/components/x-foo-test.ts.ts')).to.not.exist;

expect(_file('app/components/x-foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/x-foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'x-foo',
componentInvocation: 'XFoo',
},
})
);
});
});

it('component foo/x-foo', function () {
return emberGenerateDestroy(['component', 'foo/x-foo'], (_file) => {
expect(_file('app/components/foo/x-foo.ts')).to.not.exist;
expect(_file('app/components/foo/x-foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo/x-foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo/x-foo',
componentInvocation: 'Foo::XFoo',
},
})
);
});
});

it('component foo/x-foo --component-class="@glimmer/component"', function () {
return emberGenerateDestroy(
['component', 'foo/x-foo', '--component-class', '@glimmer/component'],
(_file) => {
expect(_file('app/components/foo/x-foo.ts')).to.equal(
glimmerComponentContents('FooXFoo')
);
expect(_file('app/components/foo/x-foo.hbs')).to.equal('{{yield}}');

expect(_file('tests/integration/components/foo/x-foo-test.ts')).to.equal(
fixture('component-test/rfc232-template.ts', {
replace: {
component: 'foo/x-foo',
componentInvocation: 'Foo::XFoo',
},
})
);
}
);
});
});
});
26 changes: 26 additions & 0 deletions node-tests/fixtures/component-test/rfc232-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'my-app/tests/helpers';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | <%= component =%>', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.set('myAction', function(val) { ... });

await render(hbs`<<%= componentInvocation =%> />`);

assert.dom().hasText('');

// Template block usage:
await render(hbs`
<<%= componentInvocation =%>>
template block text
</<%= componentInvocation =%>>
`);

assert.dom().hasText('template block text');
});
});
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@
"lint:eslint:fix": "npm-run-all \"lint:eslint --fix\"",
"lint:fix": "npm-run-all lint:*:fix",
"test": "node bin/run-tests.js",
"test:blueprints:js": "EMBER_TYPESCRIPT_BLUEPRINTS=false pnpm test:blueprints:ts",
"test:blueprints:ts": "mocha node-tests/blueprints/**/*-test.js",
"test:blueprints": "pnpm test:blueprints:js && pnpm test:blueprints:ts",
"test:blueprints:js": "EMBER_TYPESCRIPT_BLUEPRINTS=false pnpm test:blueprints:js-from-ts",
"test:blueprints:js-from-ts": "mocha node-tests/blueprints/**/*-test.js",
"test:blueprints:ts": "mocha node-tests/blueprints-ts/**/*-test.js",
"test:blueprints": "npm-run-all test:blueprints:*",
Windvis marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this one now since we no longer use it in CI, and I don't expect anyone to run all of them like this locally either.

"test:node": "qunit tests/node/**/*-test.js",
"test:browserstack": "node bin/run-browserstack-tests.js",
"test:wip": "vite build --mode development --minify false && testem ci",
Expand Down Expand Up @@ -170,6 +171,9 @@
"pnpm": {
"overrides": {
"rollup": "^4.2.0"
},
"patchedDependencies": {
"[email protected]": "patches/[email protected]"
}
},
"peerDependencies": {
Expand Down
Loading