不要怂,就是干,撸起袖子干!

Commit c8c76d43 by Constantin Metz Committed by GitHub

test(types): add tests for `.previous` and `.set` (#13129)

1 parent de5f21dc
Showing with 36 additions and 3 deletions
...@@ -27,7 +27,7 @@ const User = sequelize.define<UserModel>( ...@@ -27,7 +27,7 @@ const User = sequelize.define<UserModel>(
); );
async function test() { async function test() {
expectTypeOf<UserModel>().toMatchTypeOf(new User()); expectTypeOf<UserModel>().toMatchTypeOf(User.build());
const user = await User.findOne(); const user = await User.findOne();
expectTypeOf(user).toEqualTypeOf<UserModel | null>(); expectTypeOf(user).toEqualTypeOf<UserModel | null>();
...@@ -60,7 +60,7 @@ UntypedUser.customStaticMethod = () => {}; ...@@ -60,7 +60,7 @@ UntypedUser.customStaticMethod = () => {};
async function testUntyped() { async function testUntyped() {
UntypedUser.customStaticMethod(); UntypedUser.customStaticMethod();
expectTypeOf<UntypedUserModel>().toMatchTypeOf(new UntypedUser()); expectTypeOf<UntypedUserModel>().toMatchTypeOf(UntypedUser.build());
const user = await UntypedUser.findOne(); const user = await UntypedUser.findOne();
expectTypeOf(user).toEqualTypeOf<UntypedUserModel | null>(); expectTypeOf(user).toEqualTypeOf<UntypedUserModel | null>();
......
import { expectTypeOf } from "expect-type"; import { expectTypeOf } from "expect-type";
import { Association, BelongsToManyGetAssociationsMixin, DataTypes, HasOne, Model, Sequelize } from 'sequelize'; import { Association, BelongsToManyGetAssociationsMixin, DataTypes, HasOne, Model, Optional, Sequelize } from 'sequelize';
import { ModelDefined } from '../lib/model';
expectTypeOf<HasOne>().toMatchTypeOf<Association>(); expectTypeOf<HasOne>().toMatchTypeOf<Association>();
class MyModel extends Model { class MyModel extends Model {
...@@ -151,3 +152,35 @@ Actor.belongsToMany(Film, { ...@@ -151,3 +152,35 @@ Actor.belongsToMany(Film, {
paranoid: true paranoid: true
} }
}); });
interface ModelAttributes {
id: number;
name: string;
}
interface CreationAttributes extends Optional<ModelAttributes, 'id'> {}
const ModelWithAttributes: ModelDefined<
ModelAttributes,
CreationAttributes
> = sequelize.define('efs', {
name: DataTypes.STRING
});
const modelWithAttributes = ModelWithAttributes.build();
/**
* Tests for set() type
*/
expectTypeOf(modelWithAttributes.set).toBeFunction();
expectTypeOf(modelWithAttributes.set).parameter(0).toEqualTypeOf<Partial<ModelAttributes>>();
/**
* Tests for previous() type
*/
expectTypeOf(modelWithAttributes.previous).toBeFunction();
expectTypeOf(modelWithAttributes.previous).toBeCallableWith('name');
expectTypeOf(modelWithAttributes.previous).parameter(0).toEqualTypeOf<keyof ModelAttributes>();
expectTypeOf(modelWithAttributes.previous).parameter(0).not.toEqualTypeOf<'unreferencedAttribute'>();
expectTypeOf(modelWithAttributes.previous).returns.toEqualTypeOf<string | number | undefined>();
expectTypeOf(modelWithAttributes.previous('name')).toEqualTypeOf<string | undefined>();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!