cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html>
<head>
  <meta charset="utf-8">
  <title>Using expect and deep-freeze</title>
<!--   <script src="https://wzrd.in/standalone/expect@latest"></script>
  <script src="https://wzrd.in/standalone/deep-freeze@latest"></script> -->
</head>
<body>
</body>
</html>
            
          
!
            
              //reducer
const todos = (state = [], action) => {
	//受け取ったアクションのtypeによって切り替わる
	switch (action.type){
		case 'ADD_TODO':
			//stateは配列なので、その中に追加で新しいオブジェクトを与える。
			//与える新しいオブジェクトは、渡されたactionの中の情報を元に生成される
			return [
				...state,
				{
					id: action.id,
					text: action.text,
					completed: false,
				}
			];
		case 'TOGGLE_TODO':
			//mapについて
			//配列であるstateに対して.mapをすると、
			//その中の各配列の値をtodoとして、それに対して =>{}の関数を適用する
			//でreturnされた内容を配列に戻す
      return state.map(todo => {
        if (todo.id !== action.id) {
          return todo;
        }
				
				//todo.completedにtrue or false が入っているので、これをひっくり返す
				//で合成
        return Object.assign({}, todo, {completed: !todo.completed})
      });
	}
};

//deepFreezeはmutateしていないかをチェックするライブラリ
//expectはテスト用のライブラリ
const testAddTodo = () => {
	const stateBefore = [];
	const action = {
		type: 'ADD_TODO',
		id: 0,
		text: 'Test Text',
	};
	
	const stateAfter = [{
		id: 0,
		text: 'Test Text',
		completed: false
	}];
	
	//stateBrforeとactionがmutateしていないかをチェック
	deepFreeze(stateBefore);
	deepFreeze(action);
	
	//todosというファンクションを実行した時に、
	//帰ってくる値が、stateAfterと同じかを.toEqualメソッドで確認
	expect(
		todos(stateBefore, action)
	).toEqual(stateAfter);
};

const testToggleTodo = () => {
	const stateBefore = [
		{
			id: 0,
			text: "Test Text",
			completed: false
		},{
			id: 1,
			text: "Go shopping",
			completed: false
		}
	];
	
	const action = {
		type: 'TOGGLE_TODO',
		id: 0
	};
	
	const stateAfter = [
		{
			id: 0,
			text: "Test Text",
			completed: true
		},{
			id: 1,
			text: "Go shopping",
			completed: false
		}
	];
	
	deepFreeze(stateBefore);
	deepFreeze(action);
	
	expect(
		todos(stateBefore, action)
	).toEqual(stateAfter);
};

//テストを実行。問題あればコンソールにでる。(クロームのコンソールの方をチェック)
testAddTodo();
testToggleTodo();

console.log('All tests passed');
            
          
!
999px
Loading ..................

Console